Python Learning Summary file operations

Source: Internet
Author: User

From the beginning of this essay, summed up their own in the learning of Python in the various modules of the summary. Recently has been learning "python core programming", found that if only the book, turn over and forget, all, now strive to in one months, Python common basic knowledge to do a comb and summary.

This summary document and some operation of input and output, bibliography "Python Core Programming second Edition".

One, the operation of the file

The entry for the file is provided by the built-in function open () and file () to provide an initialization operation. The two built-in functions are essentially the same, interoperable, and most of the habits are more open ().

Open () Basic syntax:

File_obj = open (filename,access_mode='r', Buffering=-1)

Access_mode has ' R ', ' W ', ' A ', ' U ' types, which are easily distinguishable, representing the read, write, append, and universal newline character support. Note that when using ' R ', ' U ', you must make sure that the file is present. ' W ' mode, if the file contains data, it will be the case, so if you continue to append the data to use the ' a ' mode.

File input built-in method:

Read (): reads the bytes directly into the string and reads all if there is no size parameter given.

ReadLine (): Reads a line of open files, returned as a string.

ReadLines (): reads all rows and returns a list of strings.

File output built-in method:

Write (): writes a file containing textual data or binary data.

Writelines (): Action on the list. Line terminators are not added automatically.

Moving within files

The Seek () method can move the pointer position within a file to a different place.

Closing of files

Close (): Good programming habits, that is, after the operation is finished, call the Close method.

File iterations-----The best way to access files:

 for inch f:    :

Some other file object methods:

Flush (): Flush internal buffers

Next (): returns to the next line

Tell (): Returns the current position

Second, the operation of the file system

It mainly uses two modules, OS and Os.path, and is independent of the operating system.

Main functions of the OS module:

Mkfifo ()/mknod (): Create a named pipe or node

Remove ()/unlink (): Delete file

Rename ()/renames (): Renaming

Stat (): Returns file information

Walk (): Generate all filenames under a directory tree

ChDir ()/fchdir (): Change the current working directory

Chroot (): Change the current process root directory

Listdir (): Lists files for the specified directory

GETCWD (): Returns the current working directory

mkdir ()/makedirs (): Create a directory/multi-level directory

RmDir ()/removedirs (): Delete directory/multi-level directory

chmod (): Change permission mode

Chown (): Change Owner,group

Os.path main functions:

BaseName (): Remove path, return file name

DirName (): Return directory path

Jion (): Make the detached part a new path

Spilt (): Returns the path name, the file name tuple

Getatime ()/getctime ()/getmtime (): Return access/create/Modify Time

GetSize (): Return file size

Exists (): Specifies whether the path exists

Isabs (): Specifies whether the path is an absolute path

Isdir (): Specifies if a directory exists for a path

Isfile (): Specifies whether the path exists and is a file

     

Python Learning Summary 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.