Python note files

Source: Internet
Author: User

Python note files
1. Open and create a file # file () is used for file (name, mode [, buffering]) in file management. The name is the file name, and the name contains-open; does not exist. The creation mode is the open mode, r, r +, w, w +, a, a +, B, U, etc.
2. Read files

# Open () read file data = open ('file _ name') # open a file print (data. redline (), end = '') # read data from a data row using readline. seek (0) # Return to the start position of the file for line in dataprint (line, end = '') data. close () # close a file

File Reading method #1) read by row
f = open(“file.txt”)while True:line = f.readline()if line:print lineelse:break;f.close()

#2) multi-row reading
F = open('file.txt ') lines = f. readlines () # readlines () stores all f content for line in linesprint line

#3) One-time read ()
f = open(‘file.txt’)content = f.read()print contentf.close()


3. File writing
# Write (), writelines () write File f = file('file.txt ', 'W +') # w + read/write mode to open, delete the original, then write the new content line = ["hello \ n", "world \ n"] f. writelines (li) f. close () # append new content f = file('hello.txt ', 'a +') content = "goodbye" f. write (content) f. close ()


4. file Deletion # The OS module and OS. path module must be used. # The open () and file () Functions of the OS module are different from those of Python.
import osfile(‘hello.txt’, ‘w’)if os.path.exists(‘hello.txt’)os.remove(‘hello.txt’)


5. File Replication
# Use read () and write () to copy src = file(‑hello.txt "," r ") dst = file(‑hello2.txt", "w") dst. write (src. read () src. close () dst. close () # shutil module copyfile () function # copyfile (src, dst)folder file copy shutil.copyfile(#hello.txt ", #hello2.txt") # move (src, example "," .. /")#shshshutil.move(“hello2.txt ", “hello3.txt") # Move + rename


6. Rename the file
# Modify the rename () function of the file name OS module import osos.rename(“hello.txt ", “hi.txt") # modify the extension name import osfiles = OS. listdir (".) for filename in files: pos = filename. find (". ") if filename [pos + 1:] =" html ": newname = filename [: pos + 1] +" jsp "OS. rename (filename, newname)


7. Search for file content
# File search import ref1 = file(export hello.txt "," r ") count = 0for s in f1.readlines (): li = re. findall ("hello", s) # findall () queries the variable s and stores the search result in li. if len (li)> 0: count = count + li. count ("hello") print count, "hello" f1.close () # Replace file f1 = file(#hello.txt "," r ") f2 = file(#hello2.txt", "w ") for s in f1.readlines (): f2.write (s. replace ("hello", "hi") f1.close () f2.close ()


8. file comparison # The difflib module is used for sequence and file comparison.
9. Access to the configuration file # parsing the configuration file in the ConfigParser module of the Python standard library
10. Files and streams
# The sys module provides three basic stream objects: stdin, stdout, and stderr. import sys # stdin indicates the standard input sys of the stream. stdin = open(export hello.txt "," r ") # Read the file for line in sys. stdin. readlines () print line # stdout redirection output, save the output result to the file sys. stdout = open (r ". /hello.txt "," a ") print" goodbye "sys. stdout. close ()



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.