Python: File operations

Source: Internet
Author: User
Tags readline

Python files can be divided into text files and binary files in the form of a file organization

Each byte of a text file is placed in an ASCII code that represents a character. The binary file stores the in-memory data as it is stored in memory as if it were on disk.

Open and Close files: open (), close ()

Format: File object =open (file specifier [, open mode] [, buffer])

f = open ("c:\\a.txt","R")# Opens the file in the form of a read  # Note that the file directory must be double \ \ \

Open mode: R W a RB WB AB r+ w+ A + rb+ wb+ ab+

R mode can only output data from memory and cannot add data like in memory

W can only write data from memory to the file cannot enter data from file to memory

A add note the current position is the end of the document

r+ w+ A + can be written and read

File properties:

f = open ("file.txt","wb")# get file name F.name         #  file.txt# Get the File open way f.mode         #  "WB"  # Get Info: File closed f.closed       #  False# close File F.close         
Read and write operation

Read ()

    The
    • variable = file object. read () variable = file object. read ([count
    • re reads a string from an open file. It is important to note that the Python string can be binary data, not just text.
  " kk.txt contents: abcdefghijklmn   " #   Open file  fo = open ( " kk.txt   ", "  r+   "  = Fo.read (10 print  ( "  The string read is:    "  #   Close Open file  fo.close ()  #   output  The string read is: Abcdefghij 

ReadLine ()

    • Reads all characters from the current position to the end of the line, including the line terminator
    • Variable = file object. ReadLine () returns an empty string "" when at the end of the file

ReadLines ()

Returns a value from the current position until all rows in the end of the file form a list

Write operations

Write ()

    • Writes a string at the current position and returns the number of characters
    • The file object. Write (String) write () does not wrap after execution, and if a newline is required, it can be added \ n
f = open ("kk.txt""wb""Ya") Kumata!\nyou know,heyo"#  Close Open file f.close ()

# the Linux environment
Ya this is kumata!
You know,heyo

Writelines ()

    • Writes all the strings in the list to the current position, sequentially
    • File object. writelines (element list of strings)
Location of binary file operation files

File object. Tell (): Returns the current position

File object. Seek (offset [, reference point]), seek example see Csdn:seek ()

struct MODULE

Pack () packages the entire model and floating-point type into a binary string.

Unpack () binary packaged into integral type floating point type

Pickle module: Salting the object into binary file storage

Dump () pickle.dump (data, File object)

 import   picklelist  = ["  12   ",  " 34  " ,  " ok  " ,  "  good  "  ]f  = open ( " d: KK.PKL   ", "   WB   ") #   Binary write  pickle.dump (list,f) f.close ()  

OS Module
#file Rename rename ()Os.rename ("Current file name "," New file name ")#file Delete remove ()Os.remove ("file name")#Create a directory in the current directoryOs.mkdir ("new directory Name ")#change the current directoryOs.chdir ("directory ")#Get current directoryOS.GETCWD ()#Delete Empty directoryOs.rmdir ("directories to be deleted")

Python: File operations

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.