Day18.python file/directory

OS module###

The os module provides functional interface functions of most operating systems. When the os module is imported, it will adapt to different operating system platforms, and perform corresponding operations according to different platforms. When programming in python, you often deal with files and directories. At this time, you cannot do without the os module. This section contains The functions provided by the os module will be explained in detail

Module import

import  os

File operations###

File renaming
print(os.getcwd()) #First print the current path
# C:\Users\WJF This is the path of the senior sister, everyone’s is different
print("The directory is: %s"%os.listdir(os.getcwd()))   #The file name under this path, the file of the senior sister is too complicated, the result will not be displayed~
os.rename('oldname','newname')     #Here I have created a file named'oldname'You can create files in the empty folder.
print('Modify the directory name successfully')
# Modify the directory name successfully
print("The directory is: %s"%os.listdir(os.getcwd()))  #After the modification is successful, print the current directory and compare it with the previous one.

Comparing the directory list above, you can find that the directory name has been successfully modified

Delete Files#####
os.remove('remove.txt')  #I executed it twice here, and then an error was reported, indicating that it was deleted the first time, so that you can save you from checking whether the deletion was successful in the printed directory
---------------------------------------------------------------------------

FileNotFoundError                         Traceback(most recent call last)<ipython-input-17-3113f0689db3>in<module>---->1 os.remove('remove.txt')
FileNotFoundError:[WinError 2]The system can not find the file specified.:'remove.txt'

Folder operation###

Create folder
os.mkdir('newtest')
print("The directory is: %s"%os.listdir(os.getcwd()))  #You can see the new folder name from the following directory
Delete folder
os.rmdir('newtest')
print("The directory is: %s"%os.listdir(os.getcwd()))  #From the following directory, you can find that the folder just now is missing
Get the current directory
os.getcwd()
#' C:\\Users\\WJF'
Change the default directory
os.chdir('D:\Download')
os.getcwd()   #You can find that the default directory has changed
#' D:\\Download'
Get directory listing#####
os.listdir()  #Because there are many files in my current directory, I won&#39;t run it here.
os.listdir('D:\MD notes\python learning route')   #You can also list the directories under the specified folder
'''
[' Watermark_python01.png','Watermark_python02.png','Watermark_python03.png','Watermark_Python05.png','Watermark_Python06.png','Watermark_Python07.png','Watermark_Python08.png']'''
Rename folder

This is the same as modifying the file name. You can try it yourself.

Recommended Posts

Day18.python file/directory
Day21.Python project case
Day20.python operation database