程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python----- file

編輯:Python

file

Concept of documents
Computer files , It is a piece of data stored on a long-term storage device
Long term storage equipment includes : Hard disk 、U disc 、 mobile hard disk drive 、 Compact disc …

Role of documents

Save the data for a long time , Use when needed

How files are stored

In calculation , The file is saved on disk in binary form

Text files and binaries

text file :

 You can use text compilation software to view
It's still binary in nature
for example :python Source program

Binary

The content saved is not for direct reading , But for other software
for example : Picture file 、 Audio file 、 Video file …
Binary files cannot be viewed with text compilation software

Operation of file

The routine of operating files in the computer is very fixed , There are three steps :
1. Open file
2. read 、 Writing documents
read Read the contents of the file into memory
Write Write the contents of memory to a file
3. Close file

Functions for manipulating files / Method

stay python In the operation file, you need to remember 1 A function and 3 A way

function / Method explain open Open file , And return the file operation object read Read the contents of the file into memory write Write the specified content to a file close Close file

open The function is responsible for opening the file , And return the file object
read/write/close All three methods need to be called through a file object

read Method - - - - - Read the file

open The first parameter of the function is the file name to open ( File names are case sensitive )
If the file exists , Return the operation object
If the file doesn't exist , An exception will be thrown

read Method can read in and return all the contents of the file at once
close Method is responsible for closing the file

If you forget to close the file , It will cause system resource consumption , And it will affect subsequent access to files

Be careful : After method execution , Will move the file pointer to the end of the file

How to open a file

open The function defaults to Read only mode Issue documents , And return the file object

The grammar is as follows :

f = open(" file name ", " access ")
access explain r Open the file read-only , The file pointer will be placed at the beginning of the file , This is the default way , If the file doesn't exist , Throw an exception w Open the file in write only mode , If the file exists, it will be overwritten , If the file doesn't exist , Create a new file a Open the file in append mode , If the file already exists , The file pointer will be placed at the end of the file , If the file doesn't exist , Create a new file to write r+ Open the file read-write , The file pointer will be placed at the beginning of the file , This is the default way , If the file doesn't exist , Throw an exception w+ Open the file read-write , If the file exists, it will be overwritten , If the file doesn't exist , Create a new file a+ Open the file read-write , If the file already exists , The file pointer will be placed at the end of the file , If the file doesn't exist , Create a new file to write

** Tips :** Frequent movement of file pointers , It will affect the efficiency of reading and writing files , More often than not, development is done with read-only 、 Just write To operate the file

Read the contents of the file by line

read Method will read all the contents of the file into memory at one time by default
If the file is too large , The memory consumption will be very serious

readline Method

readline Method can read one line at a time
After method execution , Will move the file pointer to the next line , Ready to read again

Example :

# Open file 
file = open(" file name ")
while Ture:
# Read a line 
text = file.readline()
# Judge whether the content is read 
if not text:
break
# There is already a at the end of each read line "\n"
print(text, end="")
# Close file 
file.close()

Copy file

Example : Small file copy

# First open two files , file 1 There are initial contents , file 2 Is a blank file 
file1 = open(" file name 1")
file2 = open(" file name 2", "w")
# To operate 
text = file1.read()
file2.write(text)
# Close file 
file1.close()
file2.close()

Big file replication

If the source file is very, very large , It is not appropriate to read the source file at one time , So we can read it line by line 、 Write

# First open two files , file 1 There are initial contents , file 2 Is a blank file 
file1 = open(" file name 1")
file2 = open(" file name 2", "w")
# To operate 
while Ture:
text = file1.readline()
# Judge whether the content is read 
if not text:
break
file2.write(text)
# Close file 
file1.close()
file2.close()

file / Common management operations of directory

At terminal / Regular files can be executed in the file browser / Directory management operations
for example :
establish 、 rename 、 Delete 、 Change path …

stay python If you want to realize the above functions through the program , You need to import os modular
(ipython3)

File operations

Method name explain Example rename Rename the file os.rename( The source file name , Destination filename )remove Delete file os.remove( file name )

Directory operation

Method name explain Example listdir Directory listing os.listdir( Directory name )mkdir Create directory os.mkdir( Directory name )rmdir Delete directory os.rmdir( Directory name )getcwd Get current directory os.getcwd()chdir Modify working directory os.chdir( Target directory )path.isdir Determine if it's a document os.path.isdir( File path )
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved