"6" Python core programming chapter Nineth-File and input/output

Source: Internet
Author: User
Tags create directory

1. List of built-in methods for file objects
1 method actions for file Objects2 file.close () Close file3 File.fileno () returns the descriptor of the document (file descriptor, FD, integer value)4 File.flush () flush the internal buffer of the file5 File.isatty () Determine if file is a class TTY device6 File.nexta () returns the next line of the file (similar to File.readline ()), or when there are no other rows7 Throw Stopiteration Exception8File.read (size=-1) reads a size byte from the file, and when no size or given negative value is given, the read9 take all the remaining bytes and return as a stringTen File.readintob (buf, size) reads a size byte from a file to the BUF buffer (not supported) OneFile.readline (size=-1) to read from the file and return a row (including the line terminator), or to return the maximum size characters AFile.readlines (sizhint=0) Read all the lines of the file and return as a list (contains all line ends - characters); If the given Sizhint is greater than 0, then the sum is returned about - the sizhint byte of the row (size is determined by the next value of the buffer capacity) ( the if the buffer size can only be a multiple of 4K, if the Sizhint is 15k, then - the last possible return is 16k ——— translator Press) - File.xreadlinesc () a more efficient way to iterate and replace ReadLines () -File.seek (off, whence=0) moves the file pointer in the file, from whence (0 for the file, 1Generation +Table current position, 2represents end of file) offset off byte - File.tell () returns the current position in the file +File.truncate (size=File.tell ()) intercepts the file to the maximum size byte, which defaults to the current file location A File.write (str) writes a string to a file at file.writelines (SEQ) writes a string sequence seq to a file, and the SEQ should be a return string -An iterative object; Before 2.2, it was just a list of strings
2. File/directory Access function for OS module
1 function Description2 file Processing3Mkfifo ()/mknod () a creates a named pipe/creating a file System node4Remove ()/unlink () Delete file deletes files5Rename ()/renames () b renaming a file6*STATC () returns file information7 symlink () Creating Symbolic links8 utime () Update timestamp9Tmpfile () creates and opens ('w+b') A new temporary fileTen Walk () a generates all filenames under a directory tree OneDirectoryfolder AChDir ()/fchdir () a changes the current working directory/change the current working directory with a file descriptor - chroot () d changes the root directory of the current process - listdir () lists files for the specified directory theGETCWD ()/getcwdu () a returns to the current working directory/functions the same, but returns a Unicode object -mkdir ()/makedirs () Create directory/Create a multi -level catalog -RmDir ()/removedirs () Delete directory/Delete a multi -level catalog -AccessPermissions + access () Verify permission mode - chmod () Change permission mode +Chown ()/lchown () a changes owner and group ID/functions the same, but does not track links A umask () Set default permission mode at File Descriptor Operations - open () underlying operating system open (for files, use the standard built-in open () function) -Read ()/write () reads according to the file descriptor/Write Data -DUP ()/dup2 () Copy the file description symbol/functions the same, but is copied to another file descriptor - Device number - Makedev () a create an original device number from the major and minor device numbers inMajor () A/minor () a Major/minor device number obtained from the original device number
3. Path name Access function in Os.path module
1 function Description2 Delimited3 basename () Remove directory path, return file name4 dirname () remove file name, return directory path5 join () combines parts of the separation into one path name6 Split () return (DirName (), basename ()) tuple7 splitdrive () return (drivename, pathname) tuple8 Splitext () return (filename, extension) tuple9 InformationTen Getatime () returns the last access time One getctime () returns file creation time A Getmtime () returns the last file modification time - GetSize () returns the file size (in bytes) - Enquiry the exists () specifies whether the path (file or directory) exists - Isabs () Specifies whether the path is an absolute path - Isdir () specifies whether the path exists and is a directory - Isfile () specifies whether the path exists and is a file + Islink () specifies whether the path exists and is a symbolic link - Ismount () specifies whether the path exists and is a mount point +Samefile () Two path names pointing to the same file
4. File related modules
1Base64 provides the encoding between the binary string and the text stringdecoding Operation2BINASCII provides the encoding between binary and ASCII encoded binary stringsdecoding Operation3 BZ2A Accessing compressed files in BZ2 format4 Csva Accessing a CSV file (comma delimited file)5 FILECMPB for comparing directories and files6 fileinput A row iterator that provides multiple text files7Getopt/optparsea provides the parsing of command-line arguments/processing8glob/Fnmatch provides Unix-style wildcard matching functionality9gzip/zlib Read and write GNU zip (gzip) files (compression requires zlib module)Ten Shutil provides advanced file access capabilities Onec/Stringio providing a class file interface to a string object A Tarfilea Read and write TAR archive files, supporting compressed files - tempfile Create a temporary file (name) - encoding and decoding in UU format theZipfilec Tools for reading ZIP archive files

"6" Python core programming chapter Nineth-File and input/output

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.