Python file operations,

Source: Internet
Author: User

Python file operations,

# File operations (writing files and reading files)# Writing files #1. open the file (open the refrigerator) fp = open('test.txt ', 'w') print (fp, type (fp) #2. write files (put elephants in) fp. write ('When do you think you are no longer young? ') #3. close file (close refrigerator) fp. close () # Read the file #1. open the file (open the refrigerator) fp = open('test.txt ', 'R') # print (fp) #2. read the file (take out the elephant) txt = fp. read () print (txt) #3. close file (close the refrigerator door) fp. close () # common file functions # open () open or create a file '''open ('file path', 'open mode') # Basic Mode w write mode 1. if the file does not exist, create a new file. The file already exists. open and clear the file. 2. in this mode, only write operations can be performed in r read mode 1. if the file does not exist, an error is returned. If the file already exists, open the file and point the pointer to the beginning of the file. in this mode, only the read operation a append mode is allowed. if the file does not exist, create a new file. If the file already exists, open the file and point the pointer to the end of the file. in this mode, only write operations x xor exclusive or Mode 1 can be performed. if the file already exists, an error is returned. If the file does not exist, a new one is created. File 2. only write operations can be performed in this mode # extended mode (not used together with the basic mode) + The plus mode enables any basic mode to change to the read/write mode. the type of data to be obtained or written to the B bytes bit mode is bytes type ''' # The Enhanced Mode fp = open('wangwang.txt ', 'W + ') # write operation fp. write ('Look around ') # Set the pointer to the starting position fp. seek (0) # Read operation result = fp. read () print (result) fp. close () # The bytes mode involves the Data Type of a bytes. content = 'Why can't a comet become a police cat? 'Result = content. encode () # encoding operation print (result) fp = open('miaomiao.txt ', 'wb') fp. write (result) fp. close () fp = open('miaomiao.txt ', 'rb') result = fp. read () print (result. decode () # decode fp. close () # read () read File Content fp = open('miaomiao.txt ', 'R') # read all content # txt = fp. read () # read a specified number of characters. txt = fp. read (5) print (txt) fp. close () # write () write File Content fp = open('miaomiao.txt ', 'w') fp. write ('kqkyhtk') fp. close () # readline () reads a row of data at a time. fp = open ('Question ', 'R') result = fp. readline () result = fp. readline () # Read the first n characters of a row of data. result = fp. readline (5) print (result) fp. close () # readlines () One-time file read to the list by row to take fp = open ('Question ', 'R') # Read all rows result = fp. readlines () # Read n rows result = fp Based on the specified length. readlines (50) print (result) fp. close () # writelines () writes the data in the container to the file '''txt = ['1 \ n', '2 \ n', '3 \ n ', 'Four \ n' fp = open('num.txt ', 'w') fp. writelines (txt) fp. close () ''' # truncate () file truncation operation # fp = open('num.txt ', 'A') # The length here is measured in bytes rather than characters # fp. truncate (2) # fp. close () ''' character: A separate character is a character, no matter what language symbol byte: basic Unit for data storage 1 TB = 1024 GB 1 GB = 1024 MB 1 MB = 1024KB 1KB = 1024B 1B = 8bytes character set: Basic English encoding (1 byte) ascii code-> encoding of English and numbers and part of symbols (128) extended ascii code-> added the number of ascii codes (256) encode a specific language (2 bytes [for Chinese characters]) gb2312 encoding-> stores 5000 commonly used Chinese gb10800 encoding-> expands the number of Chinese characters gbk encoding-> contains all Chinese big5 encoding-> traditional Chinese encoding for the world's unified encoding (3 byte [for Chinese characters]) UTF-8 contains the vast majority of the world's text encoding UTF-16 contains the vast majority of the world's text encoding ''' # file function ''' # tell () Get the location of the file pointer (bytes) fp = open('01.txt ', 'R') # output current pointer position pos1 = fp. tell () print (pos1) # Read content result = fp. read () print (result) # output current pointer position pos2 = fp. tell () print (pos2) fp. close () ''' # seek () Move the file pointer position fp = open('01.txt ', 'R') # obtain the start file pointer position print (fp. tell) # Move the pointer fp. seek (9) # obtain the start file pointer position print (fp. tell () # Read File result = fp. read () print (result) fp. close ()

 

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.