Python Operations on files

Source: Internet
Author: User

Rule: Open (file_name[,access_mode][,buffering])

Parameter description

file_name:, file path + file name, add path from the beginning of the path to access, without the direct access to the path of the py file with you to edit the file in the same directory Access_mode: How to open the file: The default is read-only mode, r other ways to open the file:

' R ': Read-only

' W ': Write

' A ': Append

' r+ ' = = r+w: Readable writable, mainly read, if the file does not exist will appear to save

' w+ ' = = w+r: Readable and writable, mostly write, create a file if it does not exist

' A + ' ==a+r: can be appended to write, if the file does not exist to create

If it is a binary file, add a B to the back, for example: WB

The file operation should pay attention to the encoding format, otherwise it will appear garbled. The default format is Utf-8

1 #--*--coding:utf-8--*--2 3 #open a file and read a file4f = Open (r'File.text')5 Print(F.read ())6 f.close ()7 #Result: Your hair is crazy, it's crazy.8 #Open opens, read reads the contents of the file, close closes the file9 Ten  One #open a file that does not exist A #f = open (R '/user/xxx/s.text ') - #results: filenotfounderror: [Errno 2] No such file or directory: '/user/xxx/s.text ' -  the  - #If you open the file directly, you must close the file, when you write it will not write the contents of the - #when using with open, you don't need to close the file. -With open (r'File.text') as FP: +     Print(Fp.read ()) - #Result: Your hair is crazy, it's crazy. + #FP is the abbreviation for the file, the FP instead of the file A  at  - " " - three ways to read text: - Read : Reading all the contents of the text - ReadLine: Reading a line of an article - ReadLines () automatically parses the contents of a file into a list of rows to read in " " -  to  +  - #Write a file theWith open ('Learning.txt','W') as FP: *Fp.write ('Hello, world!.') $ #Result: Automatically creates a new Learning.txt file and stores the content Hello, world! Panax Notoginseng  - " " the There are two ways to write text: + Write (): Writes content to text A writelines (): Actions for the list the " " +  -  $ #illustrate ReadLines and Writelines $With open ('Text.txt','W') as FP: -Fp.writelines (['123\n','234\n','345\n','456\n',]) - #Results: the     " " - 123Wuyi 234 the 345 - 456 Wu     " " -  AboutWith open ('Text.txt','R') as FP: $     Print(Fp.readlines ()) - #results: [' 123\n ', ' 234\n ', ' 345\n ', ' 456\n '] -  -  A #append content after text with a mode, or directly overwrite the previous content +With open ('Learning.txt','a') as FP: theFp.write ('\nhello, world!') - #Results: $ " " the Hello, world!. the Hello, world!. the " "

Python Operations on files

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.