One, the OS module commonly used file processing function
The OS module provides an excuse function for the system environment, files, directories, and other operating system levels.
1.os.access (Path,mode): Determine if the file under Path has permission mode, or return FALSE if it returns true
[Parameter description]:
Access path to the path--file
mode--Permissions
Parameter values for mode |
Describe |
Os. F_ok |
Test if the file under path exists |
Os. R_ok |
Test if the file under path is readable |
Os. W_ok |
Test if the file under path is writable |
Os. X_ok |
Test if the file under path is executable |
1 ImportOS2 #assume that the Example.txt file exists and has read and write permissions3 Print(Os.access (R'D:\example.txt', OS. F_OK)) #判断文件example. txt is present 4 Print(Os.access (R'D:\example.txt', OS. R_OK)) #判断文件example. txt has read access 5 Print(Os.access (R'D:\example.txt', OS. W_OK)) #判断文件exampl. Txxt If write access is available 6 Print(Os.access (R'D:\example.txt', OS. X_OK)) #判断文件example. txt is executable 7 #assuming that the Tom.txt file does not exist8 Print(Os.access (R'D:\tom.txt', OS. F_OK))9 Print(Os.access (R'D:\tom.txt', OS. X_OK))
2.os.chdir (path): changes the current working directory to the specified path
[Parameter Description]:path--the new path to switch to
[Return value]: False if change is allowed to return true
1 ImportOS2 #view current working directory3Cwd=OS.GETCWD ()4 Print('the current working path is:', CWD)5 #Modify working Directory6Os.chdir (R'D:\workspace')7 #View the modified working directory8Cwd=OS.GETCWD ()9 Print('the modified working path is:', CWD)
Python: File Operations Summary 2--module operation