Python: File Operations Summary 2--module operation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.