Python common file read/write

Source: Internet
Author: User
Read text file

input = open (' Data ', ' R ')

#第二个参数默认为r

input = open (' Data ')

Read binary files

input = open (' Data ', ' RB ')

Read fixed byte

File_object = open (' Abinfile ', ' RB ')

3. Writing files

Write a text file

Output = open (' Data ', ' W ')

Write a binary file

Output = open (' Data ', ' WB ')

Append Write file

Output = open (' Data ', ' w+ ')

Write Data

File_object = open (' Thefile.txt ', ' W ')

File_object.write (All_the_text)

File_object.close ()

File read/write mode full version:

R+ has read and write properties, start writing from the file header, preserving content that is not covered in the original file;

The w+ has read and write properties, and when written, if the file exists, it is emptied and written from the beginning.

R to open a read-only file, the file must exist.

r+ open a writable file, the file must exist.

W Open Write-only file, if the file exists then the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, the file is created.

w+ Open a read-write file, if the file exists then the file length is clear to zero, that is, the contents of the file will disappear. If the file does not exist, the file is created.

A write-only file opens in an additional way. If the file does not exist, the file is created and if the file exists,

The data written will be added to the end of the file, i.e. the original contents of the file will be retained.

A + opens readable and writable files in an additional way. If the file does not exist, the file is created and if the file exists,

The written data is added to the end of the file, i.e. the original contents of the file are retained.

#!/usr/bin/env python ' maketextfile.py--Create Text file ' Import Osls = os.linesep# Get filenamefname = raw_input (' fil Ename> ') while True:if os.path.exists (fname):p rint "ERROR: '%s ' already exists"% fnameelse:break# get file content (Te XT) Linesall = []print "\nenter lines ('. ' by itself to quit). \ n" # Loop until user terminates inputwhile true:entry = Raw_ Input (' > ') if entry = = '. ': Breakelse:all.append (entry) # write lines to file with proper line-endingfobj = open (fname, ' W ') fobj.writelines (['%s%s '% (x, LS) for x @ ALL]) fobj.close () print ' done! '
#简单文本读取f = open (' Text.txt ', ' r ') for line in F.readlines ():p rint line.strip ()  #默认会读出换行符, need to be processed with strip ()
#二进制文件复制import OSF = open (' test.jpg ', ' RB ') targetfile= ' test2.jpg ' if Os.path.isfile (targetfile): Os.remove (targetfile ) #另存为print open (' test2.jpg ', ' WB '). Write (F.read ())
  • 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.