This article mainly introduces the Python operation files, as well as the simple copy backup. Hope to help everyone.
1.open function
Everything in Python is object, so normal. The process of opening a file is
1. Select File-Open file-edit, copy, delete, etc.-close file
In Python, the code implementation is:
f = open (file, mode= ' R ', Buffering=none, Encoding=none, Errors=none, Newline=none, closefd=true):
F.close ()
Where file is the absolute path plus filename, mode is the file read mode, the default is R is read-only mode, followed by the optional
Mode in the source code is interpreted as
' R ' open for reading (default) ' W ' open for writing, truncating the file first ' x ' create a new file and open It for writing ' a ' open for writing, appending to the end of the file if it exists ' B ' binary mode ' t ' text mo De (default) ' + ' open a disk file for updating (reading and writing) ' U ' Universal newline mode (deprecated)
Suggest that you look at the source code: Translation comes out is:
R opens the file in read-only mode. The pointer to the file will be placed at the beginning of the file. This is the default mode.
RB opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
r+ open a file for read-write. The file pointer will be placed at the beginning of the file.
rb+ opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file.
W opens a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
WB opens a file in binary format for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
w+ open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
wb+ opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
A opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
AB opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
A + opens a file for read and write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write.
ab+ opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write.
Here we have to call the F.close method every time the file is called the trouble is easy to forget, here with the optimization:
With open ("E:\githubproject\Source-code\basis\file\test.txt", mode= ' R ') as F:
Pass
Here f is equivalent to open the file, but at this time did not read the file, that is not put the file in memory, F has a lot of built-in methods, more commonly used is f.write ()
Here we use Fwrite to make the file copy:
With open ("E:\githubproject\Source-code\basis\file\test.txt", mode= ' R ') as F:
Contents = F.read ()
With open ("E:\githubproject\Source-code\basis\file\test_bak.txt", Mode= ' W ') as F_bak:
F_bak.write (contents)
But this method is written every time, so we use a function to encapsulate the file name in.
def CP (PATH):
With open (path, ' R ') as F:
data = F.read ()
filename = Path[0:path.rindex (".")] # is obtained by means of the Rindex method. The previous string (that is, the file name)
ext = Path[path.rindex ("."):] # is obtained by means of the Rindex method. The following string (that is, the file suffix)
With open ("%s_bak%s"% (filename, ext), ' W ') as F_bak: # New file name _bak files opened and manipulated
F_bak.write (data)
Path = "E:\githubproject\Source-code\basis\file\test.txt"
Path = Path.replace ("\", "/") # converts the string containing \ into/, avoiding the issue of special character conversion errors
Path = '/'. Join (path.split (' \ ')) #与上方法类似, but cannot convert special characters ...
CP (PATH)
There is no solution to the problem of combining filenames and paths in Windows into special characters.
We write the file to memory when we call the Read method, but what if we want to copy a large file, like 10 g?
Python file operation has a pointer to the argument that when we read to somewhere, the pointer will refer to the place read, when we read (), the file points to the end, when read (100), the pointer points to 100, the next time read from here, F.seek (0 , 0) to return the pointer to the initial position, we can use the pointer to multiple reads to achieve large file replication:
def CP (PATH): filename = Path[0:path.rindex (".")] # obtained by means of the Rindex method. The previous string (that is, the file name) ext = Path[path.rindex ("."):] # is obtained by means of the Rindex method. After the string (that is, the file suffix) with open (path, ' R ') as F, open ("%s_bak%s"% (filename, ext), ' a ') as F_bak: While True: data = F.read (1024x768) print (data) f_ Bak.write (data) if Len (data) = = 0: breakpath = "E:\githubproject\Source-code\basis\file\test.txt" path = Path.replace ("\", "/") # Converts the string containing \ into/, avoiding the issue of special character conversion errors
Path = '/'. Join (path.split (' \ ')) #与上方法类似, but cannot convert special characters ...
CP (PATH)
This article focuses on Python operations files, as well as simple copy backups.
1.open function
Everything in Python is object, so normal. The process of opening a file is
1. Select File-Open file-edit, copy, delete, etc.-close file
In Python, the code implementation is:
f = open (file, mode= ' R ', Buffering=none, Encoding=none, Errors=none, Newline=none, closefd=true):
F.close ()
Where file is the absolute path plus filename, mode is the file read mode, the default is R is read-only mode, followed by the optional
Mode in the source code is interpreted as
' R ' open for Reading (default) ' W ' open for writing, truncating the file First ' x ' Create a new file and open it for writing ' a ' open for writing, appending to the end of the file if It exists ' B ' binary mode ' t ' text mode (default) ' + ' open a disk file for updating (reading and writing ) ' U ' universal newline mode (deprecated)
Suggest that you look at the Source: translation is:
R to open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode.
RB opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
R+ opens a file for read-write. The file pointer will be placed at the beginning of the file.
Rb+ opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file.
W opens a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
WB opens a file in binary format for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
w+ opens a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
Wb+ opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
A opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
AB opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
A + opens a file for read-write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write.
Ab+ opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write.
Here we have to call the F.close method every time we open the file, it is easy to forget the trouble, here with the optimization:
with open ("E:\githubproject\Source-code\basis\file\ Test.txt ", mode= ' R ') as F:
Pass
Here F is equivalent to open the file, but at this time did not read the file, that is, the file is not put into memory, F has a lot of built-in methods, more commonly used is f.write ()
Here we use Fwrite to make the file copy:
With open ("E:\githubproject\Source-code\basis\file\test.txt", mode= ' R ') as F: contents = F.read () with open (" E:\githubproject\Source-code\basis\file\test_bak.txt ", mode= ' W ') as F_bak: f_bak.write (contents) But this method is written every time, So we use a function to encapsulate the file name in. def CP (PATH): with open (path, ' R ') as F: data = f.read () filename = Path[0:path.rindex (".")] # obtained by means of the Rindex method. The previous string (that is, the file name) ext = Path[path.rindex ("."):] # is obtained by means of the Rindex method. The string after (that is, the file suffix) with open ("%s_ bak%s "% (filename, ext), ' W ') as F_bak: # New file name _bak files open and operate f_bak.write (data) path =" E:\githubproject\ Source-code\basis\file\test.txt "path = Path.replace (" \ ","/") # Converts the string containing \ into/, avoiding the issue of special character conversion errors
Path = '/'. Join (path.split (' \ ')) #与上方法类似, but cannot convert special characters ...
CP (PATH)
There is no solution to the problem of combining filenames and paths in Windows into special characters.
We write the file to memory when we call the Read method, but what if we want to copy a large file, like 10 g?
Python file operation has a pointer to the argument that when we read to somewhere, the pointer will refer to the place read, when we read (), the file points to the end, when read (100), the pointer points to 100, the next time read from here, F.seek (0 , 0) to return the pointer to the initial position, we can use the pointer to multiple reads to achieve large file replication:
def CP (PATH): filename = Path[0:path.rindex (".")] # obtained by means of the Rindex method. The previous string (that is, the file name) ext = Path[path.rindex ("."):] # is obtained by means of the Rindex method. After the string (that is, the file suffix) with open (path, ' R ') as F, open ("%s_bak%s"% (filename, ext), ' a ') as F_bak: While True: data = F.read (1024x768) print (data) f_ Bak.write (data) if Len (data) = = 0: breakpath = "E:\githubproject\Source-code\basis\file\test.txt" path = Path.replace ("\", "/") # Converts the string containing \ into/, avoiding the issue of special character conversion errors
Path = '/'. Join (path.split (' \ ')) #与上方法类似, but cannot convert special characters ...
CP (PATH)