python2.7.13 standard library file directory operations and file operations

Source: Internet
Author: User

Chinese reference document for standard library: http://python.usyiyi.cn/translate/python_278/library/index.html

Official standard library Document: https://docs.python.org/2/

This article refers to the link: http://www.cnblogs.com/zhangfei/archive/2013/06/02/3114354.html

http://blog.csdn.net/chun_1959/article/details/45071861

Http://www.cnblogs.com/juandx/p/4962089.html (various read and write modes with files)

File directory operations:

The OS, Os.path, and Shutil modules provide directory operations, not all of which are listed here, just the usual methods.

Os.remove (path): Delete path the File,path specified by path can be a relative or absolute path, such as Os.remove (' Word.doc ') deletes the Word document under current directory; That cannot be used to delete a directory

Os.rmdir (PATH): Removes a directory that requires no file or directory under directory, or empty directory

Os.removedirs (PATH): Recursively delete subdirectories and parent directories, Os.removedirs (R ' 1\2\4\5 '), delete all directories on this path until the non-empty directory stops, if 1 has 2 and other files,1 will not be deleted, that is, only empty directories are deleted ;

Os.removedirs (R ' 1\2 ') will get an error, because there are 2 under the table 4

To delete a directory and its subdirectories and all files, call the more advanced, more abstract methods in the Shutil (Shell Utilitys) module

Os.mkdir (): Create a directory that cannot be used to create a file, such as txt text

f = open (Str,mode) can create a file, such as open ("Word.doc", ' W ') opened in write-only mode, if the file does not exist, it will be created under the current directory, F.write () write a string to the Word document, F.read () will error Because write-only mode

OS.GETCWD (): Returns a str that represents the current directory

Os.listdir (): Lists all files (directory and file) under the directory, such as Os.lsitdir (OS.GETCWD ())

Os.chdiir (dirname): Changing the working directory to DirName

Os.path.abspath (name): Get absolute path

The Os.path.split (p) function returns the directory name and file name of a path.

The Os.path.isfile () and Os.path.isdir () functions respectively verify that the given path is a file or a directory

File IO operations:

UNIX's philosophy is "Everything is a file", the mode of manipulating files is "open-read/write-close".

f = open ("Hcf.txt", ' R '), opens the existing file, read-only mode, F for the returned file object

F.read () returns a string for the contents of the file

F.close () closed the file

Because the write operation of the file will use buffering technology, that is, the characters are written to the memory buffer, without writing to the hard disk, until the buffer is full and then written, Os.flush () empties the buffer, so that all characters are written to the hard disk

python2.7.13 standard library file directory operations and file operations

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.