Through the Difflib module to achieve differences in file content, it is the standard library module python, does not need to be installed separately, the role is to compare the differences between files. and the output HTML document is supported,
First, the basic process of file operationThe computer system is divided into three parts: the hardware, the operating system and the application.Applications that we write in Python or other languages need to be saved on the hard drive if we want
first, the document operation process1. Open the file, get the file handle and assign a value to a variable2. Manipulating files with file handles3. Close the fileSecond, the file open modeF = open ("File_test", "R",encoding = "Utf-8") #打开文件, "
File mode:The mode of opening the file is:
R, read-only mode (default).
W, write-only mode. "unreadable; not exist; create; delete content;"
A, append mode. "Readable; not exist" create; "only append content;"
"+" means you can read
1 #-*-coding:utf-8-*-2 " "3 Created on April 20, 20154 5 @author: Liuxue6 7 " "8 ImportCSV9 ImportSYSTen Reload (SYS) OneSys.setdefaultencoding ('Utf-8') A " " -Valuelist=[[]], double-decker list, each element represents a row of data - " " the
1.Print to screen: printYou can pass 0 or more comma-separated expressions to print, print converts the passed expression into a string expression, and writes the result to standard output:# -*- coding: UTF-8 -*-
File Operation IntroductionPython uses the open () function, which invokes the open () function, and the application initiates a system call to open (...) to complete the operation of the file. The following examples are completed in the PYTHON3
Use Pycharm written code, each time to use the pycharm to do more trouble, packaged as an. exe program can be run directly, can also be sent to other computers to execute, more convenient.
1. Open the cmd Command Prompt window, CD to the
Open File
function open ()
Parameter description:
File: path to files
Mode: file read and write mode, default ' R ', read-only mode;
Buffering: Set buffer policy, 0 for binary file, 1 for row buffer, for text mode;
Python file reading and writing is determined by the parameters of the open () function to read and writeOpen (file, mode= ' R ', Buffering=none, Encoding=none, Errors=none, Newline=none, Closefd=true)The Open function defaults to r mode as
Write---> Write stringWritelines---> Write string sequence (sequence: a bunch of strings, separated by commas. For example: dictionaries, lists, tuples)File_obj.write (Content_obj)Content_obj + ' \ n ' #me: If you do not add \ n, the content written
#coding =utf-8Import OSdef find_file (arg,dirname,files):#for I in ARG:#print IFor file in Files:File_path=os.path.join (Dirname,file)print ' File_path: ', File_pathIf Os.path.isfile (File_path) and (arg[0] in file or arg[1] in file):print ' file:%s\
#_ *_ Coding:utf-8 _*_# author Choco#f = open ("J:\Python\py\contact.txt", encoding= "Utf-8"). Read ()#print (f) #读取整个文件内容f = open ("J:\Python\py\contact.txt", "r+", encoding= "Utf-8") #赋值给f, for file handle R Read, W write Open is to create empty
f = open ('pyehon test. txt');#Py2 can use the open or the file method for opening files, Py3 can only use open. #without arguments, open defaults to ' R ', reading, read-only mode, cannot be written and the file must exist, otherwise throws an
1 #Filename:file_read_and_write.py2 #Open the file, cnames read all the rows, store them in the list, and loop each line at the beginning with the ordinal number 1,2,3,43With open (r'File/companies.txt') as F1:4CNAMEs =F1.readlines ()5
1. Simply read the contents of the file (codecs use)There is a 1.txt document in the current directoryTo open a file:Import Codecs(1) Open filef = codecs.open (' 1.txt ')(2) file operation (read or write)Print (F.read ())(3) Close the fileF.close
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.