Python file operation supplement

Just take the big brothers and get it ready.

Rollen Holt  《About Python file operation “

AllenW     《PythonRead and write files

 

—————————————Rollen Holt—————————————

pythonThe operation of files and folders (file operation functions) involves OS modules and shutil modules.

Gets the current working directory, the directory path of the current Python script: os.getcwd ()

Returns all files and directory names in the specified directory: os.listdir ()

Function to delete a file: os.remove ()

Delete multiple directories: os.removedirs (R “c:\python”)

Check whether the path given is a file: os.path.isfile ()?

Check whether the path given is a directory: os.path.isdir ()?

Is the absolute path determined: os.path.isabs ()?

Check whether the given path is really stored: os.path.exists ()

Returns the directory name and file name of a path: os. path. split () eg os. path. split (‘/ home / Swaroop / byte / code / poem. txt’) result: (‘/ home / swaro’Op/byte/code’,’poem.txt’)

Separate extension: os.path.splitext ()

Get the path name: os.path.dirname ()

Get the file name: os.path.basename ()

Run shell command: os.system ()

Read and set environment variables: os.getenv () and os.putenv ()

Gives the line terminator used by the current platform: os.linesep Windows uses’rn’, Linux uses’n’and Mac uses’r’

Indicate the platform you are using: os. name is’nt’for Windows and’posix’ for Linux / Unix users

Rename: os.rename (old, new)

Create multi-level directory: os.makedirs (R “c:\python\test”)

Create a single directory: os.mkdir (“test”)

Get file attribute: os.stat (file)

Modify file permissions and timestamps: os.chmod (file)

Terminate the current process: os.exit ()

Get file size: os.path.getsize (filename)

File manipulation:
os.mknod(“test.txt”) Create empty files
fp = open(“test.txt”,w) Open a file directly and create a file if the file does not exist.

About open mode:

describe Pattern
w Open in writing
a Open in appended mode (starting from EOF and creating new files when necessary).
r+ Open in read-write mode
w+ Open in read-write mode (see w)
a+ Open in read-write mode (see a)
rb Open in binary read mode
wb Open in binary write mode (see w)
ab Open in binary add in mode (see a)
rb+ Open in binary read / write mode (see r+)
wb+ Open in binary read / write mode (see w+)
ab+ Open in binary read / write mode (see a+)

 

 

fp.read([size]) #sizeFor the length of the read, the unit is byte.

fp.readline([size]) #If you read a line, if you define size, you may return only part of a row.

fp.readlines([size]) #Take every line of the file as a member of list and return to the list. In fact, its internal loop is realized by cyclic calling readLine (). If the size parameter is provided, size is the total length of the read content, which means that only part of the file may be read.

fp.write(str) #When STR is written to a file, write () will not add a newline character after str.

fp.writelines(seq) #Write all the contents of SEQ into files. This function is also written faithfully, and will not add anything behind each row.

fp.close() #Close the file. Python automatically closes a file when it’s not used, but there’s no guarantee of that, and it’s best to get into the habit of closing it yourself. If a file is closed after its operation, it will generate ValueError.

fp.flush() #Write the contents of the buffer to the hard disk.

fp.fileno() #Returns a long integer file label.

fp.isatty() #Is the file a terminal device file (in the UNIX System)?

fp.tell() #Returns the current location of the file operation flag, starting with the beginning of the file.

fp.next() #Returns the next line and moves the file operation flag to the next line. Use a file for for… When a statement like in file is called, the next () function is used to implement traversal.

fp.seek(offset[,whence]) #Move the operation mark to the location of offset. This offset is usually calculated relative to the beginning of a file, usually positive. But if you provide the when parameter, it’s not necessarily true. When can be calculated from scratch for zero, and 1 for the current position as the originCalculation. 2 means calculating at the end of the file as the origin. Note that if the file is opened in a or a + mode, the file action tag returns automatically to the end of the file each time a write is performed.

fp.truncate([size]) #The file is cut to a prescribed size, and the default is to cut the location of the current file operation mark. If size is larger than the size of the file, depending on the system, it may not change the file, it may be filled with 0 to the corresponding size, or it may be added with some random content.

 

Directory operation:
os.mkdir(“file”) Create a directory
Copying files:
shutil.copyfile(“oldfile”,”newfile”) oldfileAnd newfile can only be files.
shutil.copy(“oldfile”,”newfile”) oldfileIt can only be a folder, newfile can be a file, or a target directory.
Copy folder:
shutil.copytree(“olddir”,”newdir”) olddirAnd newdir can only be directories, and newdir must not exist.
Rename file (directory)
os.rename(“oldname”,”newname”) This command is used in files or directories.
Mobile files (directory)
shutil.move(“oldpos”,”newpos”)
Delete files
os.remove(“file”)
Delete directory
os.rmdir(“dir”)Only empty directory can be deleted.
shutil.rmtree(“dir”) Empty directory and content directory can be deleted.
change directory
os.chdir(“path”) Change path

 

Related examples

1 Add all the picture names under the folder to’_fc’.

pythonCode:

# -*- coding:utf-8 -*-
import re
import os
import time
#str.split(string)Split string
#'The connector'.join (list) makes the list a string.
def change_name(path):
global i
if not os.path.isdir(path) and not os.path.isfile(path):
return False
if os.path.isfile(path):
file_path = os.path.split(path) #Splitting out directories and files
lists = file_path[1].split('.') #Partition file and file extension
file_ext = lists[-1] #Remove the suffix name (list slicing operation).
img_ext = ['bmp','jpeg','gif','psd','png','jpg']
if file_ext in img_ext:
os.rename(path,file_path[0]+'/'+lists[0]+'_fc.'+file_ext)
i+=1 #Notice that I is a trap here.
#perhaps
#img_ext = 'bmp|jpeg|gif|psd|png|jpg'
#if file_ext in img_ext:
# print('ok---'+file_ext)
elif os.path.isdir(path):
for x in os.listdir(path):
change_name(os.path.join(path,x)) #os.path.join()Useful in path processing.


img_dir = 'D:\\xx\\xx\\images'
img_dir = img_dir.replace('\\','/')
start = time.time()
i = 0
change_name(img_dir)
c = time.time() - start
print('Program running time:%0.2f'%(c))
print('A total of%s pictures were processed.'%(i))

 

Output results:

Program running time: 0.11
A total of 109 images were processed.

—————————————AllenW—————————————

PythonRead and write files
1.open
After opening files with open, remember to call the close () method of the file object. For example, you can use the try/finally statement to ensure that the file can be closed at last.

file_object = open(‘thefile.txt’)
try:
all_the_text = file_object.read( )
finally:
file_object.close( )

Note: You cannot put the open statement in the try block because the file object file_object cannot execute the close () method when an exception occurs to the open file.

2.read file
Read text file
input = open(‘data’, ‘r’)
#The second parameter is r by default.
input = open(‘data’)

 

Read binary files
input = open(‘data’, ‘rb’)

Read all contents
file_object = open(‘thefile.txt’)
try:
all_the_text = file_object.read( )
finally:
file_object.close( )

Read fixed byte
file_object = open(‘abinfile’, ‘rb’)
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object.close( )

Read each line
list_of_all_the_lines = file_object.readlines( )

If the file is a text file, you can also directly traverse the file object to get each row:

for line in file_object:
process line

3.Write a document
Writing text files
output = open(‘data’, ‘w’)

Writing binary files
output = open(‘data’, ‘wb’)

Add write file
output = open(‘data’, ‘w+’)

Write data
file_object = open(‘thefile.txt’, ‘w’)
file_object.write(all_the_text)
file_object.close( )

Write multiple lines
file_object.writelines(list_of_text_strings)

Note that calling writelines to write multiple lines is more efficient than using write for one-time writes.

When dealing with log files, we often encounter situations where log files are so large that it is impossible to read the entire file into memory at once for processing. For example, we need to process a 2GB log file on a 2GB machine, and we may want to process only one log file at a timeThe content of 200MB.
In Python, the built-in File object directly provides a readlines (sizehint) function to do this. Take the following code for example:

file = open(‘test.log’, ‘r’)sizehint = 209715200 # 200Mposition = 0lines = file.readlines(sizehint)while not file.tell() – position < 0: position = file.tell() lines = file.readlines(sizehint)

Each time the readlines (sizehint) function is called, about 200 MB of data is returned, and the returned data must be complete row data. In most cases, the number of bytes of the returned data is slightly larger than the value specified by sizehint (exceptThe last time you call the readlines (sizehint) function. Typically, Python automatically adjusts user-specified sizehint values to integer times the size of the internal cache.

fileIn Python, it is a special type, which is used to operate external files in Python programs. In Python, everything is an object. File is no exception. File has file’s methods and properties. Let’s first look at how to create a file object:

file(name[, mode[, buffering]])
file()The function is used to create a file object, which has an alias called open (), perhaps more visually, and they are built-in functions. Let’s take a look at its parameters. Its parameters are passed in the form of strings. Name is the name of the file.
modeIt’s open mode, with an optional value of R W a U, representing read (default) write mode that adds support for various newlines, respectively. If you open a file in W or a mode, if the file does not exist, it will be created automatically. In addition, when using W mode to open an existing file, the original file will be inside.Volume will be emptied, because at the beginning of the file operation marker is at the beginning of the file, this time to write operation, will undoubtedly erase the original content. For historical reasons, newline characters have different patterns in different systems, such as a n in UNIX and a n in Windows’ r n’, opening a file in U mode, is to support all newline modes, which means that’ R’ n’ R \ n’can represent newlines, and there will be a tuple to store the newline characters used in the file. However, although there are many patterns in line wrap, read pytho.N is replaced by \n. Behind the pattern character, you can also add + b t to indicate that you can read and write the file at the same time and open the file in binary mode and text mode (default).
bufferingIf 0 means no buffering; if 1 means “line buffering”; if a number greater than 1 means the size of the buffer, it should be in bytes.

fileObjects have their own properties and methods. Let’s take a look at the properties of file first.

closed #Whether the marked file is closed and rewritten by close ()
encoding #File encoding
mode #Open mode
name #file name
newlines #The line wrap mode used in the file is a tuple.
softspace #booleanType, generally 0, is said to be used for print

fileMethods of reading and writing:

F.read([size]) #sizeFor the length of the read, the unit is byte.
F.readline([size])
#If you read a line, if you define size, you may return only part of a row.
F.readlines([size])
#Take every line of the file as a member of list and return to the list. In fact, its internal loop is realized by cyclic calling readLine (). If the size parameter is provided, size is the total length of the read content, which means that only part of the file may be read.
F.write(str)
#When STR is written to a file, write () will not add a newline character after str.
F.writelines(seq)
#Write all the contents of SEQ into the file. This function is also written faithfully, and will not add anything behind each row.
fileOther methods:

F.close()
#Close the file. Python automatically closes a file when it’s not used, but there’s no guarantee of that, and it’s best to get into the habit of closing it yourself. If a file is closed after its operation, it will generate ValueError.
F.flush()
#Write the contents of the buffer to the hard disk.
F.fileno()
#Returns a long integer file label.
F.isatty()
#Is the file a terminal device file (in the UNIX System)?
F.tell()
#Returns the current location of the file operation flag, starting with the beginning of the file.
F.next()
#Returns the next line and moves the file operation flag to the next line. When a file is used for statements like for… In file, it is called next () function to achieve traversal.
F.seek(offset[,whence])
#Move the operation mark to the location of offset. This offset is usually calculated relative to the beginning of a file, usually positive. But if you provide the when parameter, it’s not necessarily true. When can be calculated from scratch for zero, and 1 for the current position as the originCalculation. 2 means calculating at the end of the file as the origin. Note that if the file is opened in a or a + mode, the file action tag returns automatically to the end of the file each time a write is performed.
F.truncate([size])
#The file is cut to a prescribed size, and the default is to cut the location of the current file operation mark. If size is larger than the size of the file, depending on the system, it may not change the file, it may be filled with 0 to the corresponding size, or it may be added with some random content.

 

Code#! /usr/bin/python
import os,sys

try:
    fsock = open("D:/SVNtest/test.py", "r")
except IOError:
    print "The file don't exist, Please double check!"
    exit()
print 'The file mode is ',fsock.mode
print 'The file name is ',fsock.name
P = fsock.tell()
print 'the postion is %d' %(P)
fsock.close()

#Read file
fsock = open("D:/SVNtest/test.py", "r")
AllLines = fsock.readlines()
#Method 1
for EachLine in fsock:
    print EachLine

#Method 2
print 'Star'+'='*30
for EachLine in AllLines:
    print EachLine
print 'End'+'='*30
fsock.close()

#write this file
fsock = open("D:/SVNtest/test.py", "a")
fsock.write("""
#Line 1 Just for test purpose
#Line 2 Just for test purpose
#Line 3 Just for test purpose""")
fsock.close()


#check the file status
S1 = fsock.closed
if True == S1:
    print 'the file is closed'
else:
    print 'The file donot close'

 

Leave a Reply

Your email address will not be published. Required fields are marked *