0% found this document useful (0 votes)
24 views4 pages

OS Module

The document provides an overview of the OS module in Python, detailing its functions for file and directory manipulation, such as renaming, deleting, and checking the existence of files. It includes examples of how to create directories and handle files using the OS module, along with error handling for file operations. Additionally, it demonstrates the use of datetime for timestamping files and managing directory structures.

Uploaded by

VinuCool Vk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

OS Module

The document provides an overview of the OS module in Python, detailing its functions for file and directory manipulation, such as renaming, deleting, and checking the existence of files. It includes examples of how to create directories and handle files using the OS module, along with error handling for file operations. Additionally, it demonstrates the use of datetime for timestamping files and managing directory structures.

Uploaded by

VinuCool Vk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OS Module

In [ ]: OS==> Operating system

This module is designed for to interact with OPerating system

OS===> Windows, MAc, Linux,

os
datetime
re
math

OS modeule

In [ ]: OS Module

1. [Link]() # To rename file and move the file


2. [Link]() # To delete file
3. [Link]() # To check file is exist or not
4. [Link]() # to create single directory
5. [Link]() # to create multiple folders in tree structure
6. [Link]() # to delete empty directory
7. [Link]() # list to all files and folder in present directory
8. [Link]() # retirn path current working directory
9. [Link]() # to join multiple path

In [1]: with open('[Link]','w') as file:


[Link]('sample testing file.')

In [2]: import os

[Link]()

In [ ]: [Link]('source_file_path','destination_file_path')

In [3]: [Link]('[Link]','[Link]')

In [5]: source_file_path=r'C:\Users\gssaw\Data_Analytics_10_05_2025\Daily_class_notes/2025_06_30_OS_datetime/[Link]'
destination_file_path=r'C:\Users\gssaw\Data_Analytics_10_05_2025\Daily_class_notes/2025_06_30_OS_datetime/[Link]'

In [6]: [Link](source_file_path,destination_file_path)

move

In [7]: source_file_path=r'C:\Users\gssaw\Data_Analytics_10_05_2025\Daily_class_notes/[Link]'

In [8]: destination_file_path=r'C:\Users\gssaw\Data_Analytics_10_05_2025\Daily_class_notes/2025_06_30_OS_datetime/new_sample.txt'

In [9]: [Link](source_file_path,destination_file_path) # rename file as well change the directory

In [10]: source_file_path=r'C:\Users\gssaw\Data_Analytics_10_05_2025\Daily_class_notes/sample_text.txt'

In [11]: destination_file_path=r'C:\Users\gssaw\Data_Analytics_10_05_2025\Daily_class_notes/2025_06_30_OS_datetime/sample_text.txt'

In [12]: [Link](source_file_path,destination_file_path) # change the directory

In [ ]:

2. [Link]() # To delete file

In [ ]: [Link](file_path)

In [13]: with open('test_file.txt','w') as file:


[Link]('we are learing os module')
In [14]: [Link]('test_file.txt')

In [15]: destination_file_path=r'C:\Users\gssaw\Data_Analytics_10_05_2025\Daily_class_notes/2025_06_30_OS_datetime/sample_text.txt'

In [16]: [Link](destination_file_path)

In [18]: [Link](destination_file_path) # if file is not exist then shows FileNotFoundError

---------------------------------------------------------------------------FileNotFoundError Traceback (most recent call last)Cell In[18], l


----> 1 [Link](destination_file_path) # if file is not exist then shows FileNotFoundError
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\gssaw\\Data_Analytics_10_05_2025\\Daily_class_notes/2025_06_30_OS_datetime/sa

In [24]: import time


import datetime

In [29]: date=[Link]()
print(date)

2025-06-30 [Link].183376

In [34]:

for i in range(5):
date_1=[Link]()
[Link](2)
with open(f'sample_{str(date_1)}.txt','w') as file:
[Link](f'sample file created at time =={date_1}')

---------------------------------------------------------------------------OSError Traceback (most recent call last)Cell In[34], l


2 date_1=[Link]()
3 [Link](2)
----> 4 with open(f'sample_{str(date_1)}.txt','w') as file:
5 [Link](f'sample file created at time =={date_1}')
File ~\AppData\Roaming\Python\Python312\site-packages\IPython\core\[Link], in _modified_open(file, *args, **kwargs)
303 if file in {0, 1, 2}:
304 raise ValueError(
305 f"IPython won't let you open fd={file} by default "
306 "as it is likely to crash IPython. If you know what you are doing, "
307 "you can use builtins' open."
308 )
--> 310 return io_open(file, *args, **kwargs)
OSError: [Errno 22] Invalid argument: 'sample_2025-06-30 [Link].[Link]'

In [35]: import datetime


import time

for i in range(5):
date_now = [Link]()
[Link](2)
formatted_date = date_now.strftime("%Y-%m-%d_%H-%M-%S")
with open(f'sample_{formatted_date}.txt', 'w') as file:
[Link](f'sample file created at time == {date_now}')

In [36]: [Link]()

Out [36]: 1751250303.8484142

In [37]: import pandas as pd

In [40]: df=pd.read_csv(r'C:\Users\gssaw\Data_Analytics_10_05_2025\Daily_class_notes/2025_06_27_reduce_file_handling/[Link]')

In [41]: df

Out [41]:
Unnamed: 0 roll_no name result grade

0 0 101 virat pass A

1 1 102 rohit pass B

2 2 103 hardik pass B

3 3 104 ram fail C

4 4 105 sham fail D

3. [Link]() # To check file is exist or not

In [ ]: [Link](file_path)
True/False

In [42]: file_path='sample_2025-06-30_07-[Link]'

In [43]: [Link](file_path)
Out [43]: True

In [44]: file_path='sample_2025-06-30_07-[Link]'

In [45]: [Link](file_path)

Out [45]: False

In [46]: file_path='sample_2025-06-30_07-[Link]'
if [Link](file_path):
[Link](file_path)
print('file Deleted')
else:
print('file not found')

file Deleted

In [47]: file_path='sample_2025-06-30_07-[Link]'
if [Link](file_path):
[Link](file_path)
print('file Deleted')
else:
print('file not found')

file not found

In [48]: file_path='sample_2025-06-30_07-[Link]'
if [Link](file_path):
with open(file_path) as file:
text=[Link]()
print('text is read from file \n',text)
[Link](file_path)
print('file Deleted')
else:
print('file not found')

text is read from file


sample file created at time == 2025-06-30 [Link].701808
file Deleted

In [ ]:

4. [Link]() # to create single directory

In [ ]: [Link](folder_name)

In [49]: [Link]('gajanan')

In [51]: folder_path=r'C:\Users\gssaw\Data_Analytics_10_05_2025\Daily_class_notes/2025_06_27_reduce_file_handling/data'
[Link](folder_path)

In [52]: [Link]('Images')

In [56]: file_path=r'Images\sample_2025-06-30_07-[Link]'
folder_name='Images'
if not [Link](folder_name): #folder is available or not
[Link](folder_name) # folder creation
with open(file_path,'w') as file: # file create
[Link]('created sample text file') # text added in file
print('file created successfully') # alert
else:
print('file not created')

file not created

5. [Link]() # to create multiple folders in tree structure

In [59]: import warnings


[Link]('ignore')

In [60]: file_path=r'data_analytics\machine_learing\deep_learning\Images\sample_2025-06-30_07-[Link]'
folder_name='data_analytics\machine_learing\deep_learning\Images'
if not [Link](folder_name): #folder is available or not
[Link](folder_name) # folder creation
with open(file_path,'w') as file: # file create
[Link]('created sample text file') # text added in file
print('file created successfully') # alert
else:
print('file not created')
file created successfully

In [ ]:

6. [Link]() # to delete empty directory

In [ ]: [Link](directory_path)

In [61]: [Link]('gajanan')

In [62]: [Link]('gajanan')

---------------------------------------------------------------------------FileNotFoundError Traceback (most recent call last)Cell In[62], l


----> 1 [Link]('gajanan')
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'gajanan'

In [ ]:

7. [Link]() # list to all files and folder in present directory

In [ ]: [Link]()

In [63]: [Link]()

Out [63]: ['.ipynb_checkpoints',


'data_analytics',
'Images',
'new_sample.txt',
'OS_module.ipynb',
'[Link]',
'sample_2025-06-30_07-[Link]',
'sample_2025-06-30_07-[Link]',
'sample_2025-06-30_07-[Link]',
'sample_2025-06-30_07-[Link]']

In [64]: path=r'C:\Users\gssaw\Data_Analytics_10_05_2025'
[Link](path)

Out [64]: ['.ipynb_checkpoints',


'.virtual_documents',
'Credit Risk Modeling - Preparation - [Link]',
'Credit Risk Modeling - Preparation - [Link]',
'Credit Risk Modeling - Preparation - 4-4 (1).ipynb',
'Credit Risk Modeling - Preparation - [Link]',
'Credit Risk Modeling - Preparation - 4-5 (1).ipynb',
'Credit Risk Modeling - Preparation - 4-5 (2).ipynb',
'Credit Risk Modeling - Preparation - [Link]',
'Credit Risk Modeling - Preparation - [Link]',
'Credit Risk Modeling - Preparation - With Comments - [Link]',
'credit_risk.py',
'credit_risk_assistant',
'Daily_class_notes',
'day_1',
'day_2',
'day_3',
'day_4',
'[Link]']

In [ ]:

8. [Link]() # retirn path current working directory

In [65]: [Link]()

Out [65]: 'C:\\Users\\gssaw\\Data_Analytics_10_05_2025\\Daily_class_notes\\2025_06_30_OS_datetime'

In [67]: pwd # return present working directory

Out [67]: 'C:\\Users\\gssaw\\Data_Analytics_10_05_2025\\Daily_class_notes\\2025_06_30_OS_datetime'

In [ ]:

In [68]: !python [Link]

addition of 11 and 22 is 33

The value specified in an AutoRun registry key could not be parsed.

In [ ]:

You might also like