Python file operations and modules - Python Day11

Keywords: Python Back-end

catalogue

1. File opening and closing

2. Document reading and writing

3. Application: file backup script

4. Document positioning

5. Module introduction

6.os module operation file

7.time and datetime modules

8. Module production, release and installation

  Reading guide

File operation is a very common function. Of course, we also support python. Python has the basic operation method of files, but it is more convenient and fast with the help of os and other modules. In the learning process, we will use modules from time to time. Here we will also explain modules, common modules, and how to make, release and install modules in detail.

Learning purpose

1. Complete the file backup script by opening, closing, reading and writing files (key points)

2. Accurately control document reading and writing through relevant methods of document positioning

3. Use os module to operate files and improve efficiency (key points)

4. Use time and datetime modules to complete time related operations (key points)

5. Further understand the significance of the module through the production, release and installation of the module

1. File opening and closing

General steps for file operation

Open file

Read / write file

Save file

Close file

1.1 opening files

Open a file in Python. Using the open function, you can open an existing file or create a new file

Syntax format:

open('File name ',' open mode ')

Example:

#'w' mode
fobj=open('./Test.txt','w')
fobj.write('There is a poem on the vast sea\r\n')
fobj.close()

#Write in binary
fobj=open('./Test.txt','wb')
fobj.write('Between the dark clouds and the sea'.encode('utf-8'))
fobj.close()

#Additional data
fobj=open('./Test.txt','a')
fobj.write('Between the dark clouds and the sea\r\n')
fobj.write('Petrels are like black lightning')
fobj.close()

File open mode  

    '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)

 

 

1.2 closing documents

Syntax format:

The close() method closes the file

Example:

f = open('text.txt','w') 
f.close() 

Note: after opening a file, be sure to close it, otherwise you can't continue to operate the file later  

with context management  

The with statement can ensure that the open file handle has been closed after the execution of the with statement, regardless of whether an exception occurs during file processing.

#Write data
fobj=open('./Test.txt','w')
fobj.write('Between the dark clouds and the sea\n')
fobj.write('Petrels are like black lightning\n')
fobj.close()

#with processing
with open('Test.txt','a') as f:  #Add
    f.write('I think python very nice')

with open('Test.txt','r') as f:  #read
    print(f.read())

Operation results

Between the dark clouds and the sea
 Petrels are like black lightning
 I think python very nice

2. Document reading and writing

2.1 writing documents

Write the file write() method. The parameter is the content to be written.

2.2 reading documents

  Read the file read() and read out all the contents of the file.

fobj=open('./Test.txt','w')
fobj.write('Between the dark clouds and the sea\n')
fobj.write('Petrels are like black lightning\n')
fobj.close()
#Read data operation
f=open('Test.txt','r')
#print(f.read())# Read all data
# print(f.read(12))#Read 12 numbers
# print(f.read())#Read the rest
# print(f.readline()) #Read one line
# print(f.readline())  #Read another line
print(f.readlines())  #Read all rows and return the list

  Read the specified number of characters read(num) passes in a number as a parameter to read the specified number of characters.

content = f.read(2) # Read two characters 

  readlines() reads by line, reads all the contents at one time, and returns a list. Each line is used as an element.

content = f.readlines() # Read all contents at once and return a list. The list elements are the contents of each line 

  readline() reads by line, but only one line at a time

content = f.readline() # Read by line, one line at a time 

  Binary rb   Read method of

fobj=open('./Test.txt','w')
fobj.write('Between the dark clouds and the sea\n')
fobj.write('Petrels are like black lightning\n')
fobj.close()
#Read data operation
f=open('Test.txt','rb')  #Binary read
data=f.read()
print(data)   #Before decoding
print(data.decode('gbk'))# After decoding in gbk
f.close()  #Close file

Operation results

b'\xd4\xda\xce\xda\xd4\xc6\xba\xcd\xb4\xf3\xba\xa3\xd6\xae\xbc\xe4\r\n\xba\xa3\xd1\xe0\xcf\xf1\xba\xda\xc9\xab\xb5\xc4\xc9\xc1\xb5\xe7\r\n'
Between the dark clouds and the sea
 Petrels are like black lightning

Summary:

  Several operation modes of file reading and writing

read  r r+ rb rb+

r   r+   Read only, use normal to read the scene

rb   rb+   Apply to   file   picture   video   Audio file reading

write  w w+ wb wb+  a ab

w w+   wb wb+   Every time a new file is created, the original file is overwritten, and the binary is read and written   Pay attention to the encoding problem. By default, the encoding we write to the file is gbk

a   ab   a+   Append after the basis of the original file [end of file pointer], and a new file will not be created each time

3. Application: file backup script

demand

1. Use scripts to complete automatic backup. Users are required to input file names to complete automatic backup

def copyFile():
    #Receive the file name entered by the user
    old_file=input('Please enter the file name to be backed up:')
    file_list=old_file.split('.')
    #Construct a new file name with the suffix of the backup
    new_file=file_list[0]+'backups'+file_list[1]
    old_f=open(old_file,'r') #Open the file that needs to be backed up
    new_f = open(new_file, 'w')  #Open a new file in write mode. If it does not exist, create it
    content=old_f.read()#Read out the contents of the file
    new_f.write(content)
    old_f.close()
    new_f.close()
copyFile()

2 it is obviously inappropriate to read all the contents at one time when processing large files. Improve the code on the basis of requirement 1 so that it can back up large files without filling up the memory.

def copyBigFile():
    #Receive the file name entered by the user
    old_file=input('Please enter the file name to be backed up:')
    file_list=old_file.split('.')
    #Construct a new file name with the suffix of the backup
    new_file=file_list[0]+'backups'+file_list[1]
    try:
        #Monitor logic to process
        with open(old_file,'r') as old_f,open(new_file,'w') as new_f:
            while True:
                content=old_f.read(1024)  #1024 characters at a time
                new_f.write(content)
                if len(content)<1024:
                    break
    except Exception as msg:
        print(msg)

copyBigFile()

4. Document positioning

 tell()

File positioning refers to the position read by the current file pointer and the cursor position. In the process of reading and writing files, if you want to know the current location, you can use tell() to get it

#tell returns the current position of the pointer
with open('Test.txt','r') as f:
    print(f.read(3))
    print(f.tell())
    print(f.read(2))  #5 Chinese characters and 10 bytes in total
    print(f.tell())

  Operation results

m Description in Ukraine: English occupies 1 character and Chinese characters occupy 2 characters
5
 Yunhe
9    Description: cumulative“ m In "dark cloud and", 9 characters in total

truncate()   Retain the top * * bit   

#truncate can intercept the source file
print('----Data before interception---')
fobjB=open('Test.txt','r')
print(fobjB.read())
fobjB.close()
print('----Intercepted data---')
fobjA=open('Test.txt','r+')
fobjA.truncate(16)  #Retain the first 16 characters
print(fobjA.read())
fobjA.close()

Output results

----Data before interception---
Between the dark clouds and the sea
 Petrels are like black lightning
 I think python Very, very good
----Intercepted data---
Between the dark clouds and the sea

 seek()

If you need to navigate to another location to operate in the process of operating the file, use seek().

seek(offset, from) has two parameters, offset, offset unit byte, negative number is back offset, positive number is forward offset, from position: 0 indicates the beginning of the file, 1 indicates the current position, and 2 indicates the end of the file

with open('Test backups txt','rb') as f:
    print(f.read().decode('gbk'))
    print('-----------------------------')
    f.seek(0, 0)  #Start with the header of the file
    data=f.read(2)
    print(data.decode('gbk'))  #stay
    f.seek(-2,1) #At the current position, move 2 bits to the left, which is equivalent to setting the cursor to the position of 0
    print(f.read(4).decode('gbk'))  #After the last code, return to the initial position and read 4 bits again
    f.seek(2,1) #Move 2 characters behind 'Wu' to 'cloud'‘
    print(f.read(4).decode('gbk'))  #Read four characters after 'cloud' to 'and big'
    f.seek(-4, 2)  # Move 4 characters to the left at the end to 'normal'
    print(f.read(4).decode('gbk')) #Read four characters after 'Chang' to 'Chang Hao'

  Operation results

Between the dark clouds and the sea
 Petrels are like black lightning
 I think python Very, very good
-----------------------------
stay
 In Ukraine
 And big
 have the hobby of doing sth.

Note: it needs to be opened in binary 'rb' mode

For a file opened in "r" mode, the binary option is not used to open the file in the text file. It is only allowed to calculate the relative position from the beginning of the file. If it is calculated from the end of the file or the current calculation, an exception will be thrown

5. Module introduction

Import import module

We often say that Python has powerful third-party libraries and many common functions. Python provides its own built-in modules. Simply put, a module is a py file that encapsulates a bunch of functions. It is like a toolkit. To use the tools inside, you must first bring the toolkit. The module is the same. If you need to use the module in the program, you must import the module first.

In Python, the import module uses the import keyword. For example, we import a time module time to get the current time. Module import is usually placed at the front of the file.

Example

import time
print(time.ctime())
'''
Calling functions in modules
 Return to the present time  Sun Nov  7 15:25:59 2021
'''

  Call the method of the module, format: module name. Function name. This call can prevent errors caused by methods with the same name in different modules.

import to the time when the module is imported for the first time   The following 3 steps will occur

1. Open the module file

2. Execute the file corresponding to the module and throw all the names generated during execution into the module namespace

3. There will be a module name in the program to point to the module namespace

Search path  

When the interpreter encounters the import keyword, if the module is in the current search path, it will be imported. Check the search path imported by import. When importing a module, priority will be given to searching whether there is a module with this name in the current directory, so do not have the same name as the module in the system when naming the module.  

(1) . current directory  

(2) If the current directory does not exist, search in the environment variable. You can use the path variable in the sys module to view all paths.    

(3) If you can't find them, search the default path. For linux system, the default path is generally / usr/local/lib/python/  

From... Import method of importing module

  There may be many functions in a module. If you want to import only a few of them, you can import them by using the from xx import xx method. Advantages: there is no prefix, and the method can be used directly     Disadvantages: it is easy to duplicate names and cause conflicts

Example: only ctime and time methods in the time module can be imported in this way.

from time import ctime,time
'''
from time Import in module ctime,time
'''
print(ctime())  #Use directly without adding time.ctime()
print(time())

  Output results

Sun Nov  7 15:40:26 2021
1636270826.3767643

Using from import, if the function names are the same, the later imported will overwrite the previous imported.  

Import all functions in the module at one time  

Syntax format: from xxx import*  

Example:

from time import *

Alias module as  

Sometimes the imported module name is very long and inconvenient to call. You can use as to alias the module.

import time as myTime  #Aliasing the module invalidates the original time
print(myTime.ctime()) #You can't use the original time name

When calling, it is not difficult to use the original name. You should use an alias  

6.os module operation file

summary  

Rename and delete files. You can use the os module in python. The os module provides some system level operation commands

os.getcwd() # returns the current working directory

os.chdir(path) # change the working directory

os.listdir(path = ".") # enumerates the file names in the specified directory (". Indicates the current directory and".. "indicates the upper level directory

os.mkdir(path) # creates a directory. If the directory already exists, an exception will be thrown

os.makedirs(path) # recursively creates a multi-layer directory. If the directory already exists, an exception is thrown

os.remove(path) # delete file

os.rmdir(path) # deletes a single-layer directory. If the directory is not empty, an exception will be thrown

OS. Removediers (path) # recursively deletes the directory, trying to delete it layer by layer from the subdirectory to the parent directory. If the directory is not empty, an exception will be thrown

os.rename(old,new) # rename the file old to new

os.system(command) # runs the system shell command

os.walk(top) # traverses all the following subdirectories of the top path and returns a triple: (path, [including directory], [including file])

The os.curdir # attribute indicates the current directory

os.pardir # attribute, indicating the upper level directory

os.sep # attribute, which outputs the operating system specific path separator ('\ \' under win and '/' under Linux)

os.linesep # attribute, the line terminator used by the current platform ('\ r\n' under Win and '\ n' under Linux)

os.name # attribute refers to the operating system to be used currently

Path method

os.path.basename(path) # remove the directory path and return the file name separately
os.path.dirname(path) # remove the file name and return the directory path separately

os.path.join(path1[,],path2 [,...]) # combines the parts of path1 and path2 into a pathname

os.path.split(path) # splits the file name and path, and returns a (f_path,f_name) tuple,

  #  If you use a directory entirely, it also separates the last directory as a file name

Os.path.splittext (path) # separates the file name and suffix, and returns (f_name,f_extension) tuples,

  #  If you use a directory entirely, it also separates the last directory as a file name

os.path.getsize(file) # returns the size of the specified file in bytes

os.path.getatime(file) # returns the most recent access time of the specified file

  # (floating point seconds, which can be converted by gmtime() or localtime() function of time module)

Os.path.gettime (file) # returns the creation time of the specified file

os.path.getmtime(file) # returns the latest modification time of the specified file

os.path.exists(path) # determines whether the specified path exists (directory or file)

os.path.isabs(path) # determines whether it is an absolute path

os.path.isdir(path) # determines whether the specified path exists and is a directory

os.path.isfile(path) # determines whether the specified path exists and is a file

os.path.islink(path) # determines whether the specified path exists and is a symbolic link

os.path.ismount(path) # determines whether the specified path exists and is a mount point

os.path.samefile(path1,path2) # judge whether path1 and path2 point to the same file

  Modify file name: rename (file name to be modified, new file name)

import os
#Modify file name: Rename (file name to be modified, new file name)
os.rename(r'C:\Users\MLoong\Desktop\89.xlsx',r'C:\Users\MLoong\Desktop\98.xlsx')

Delete file: remove (file name to be deleted)  

import os
os.remove(r'C:\Users\MLoong\Desktop\98.xlsx')

Create folder: MKDIR (folder name)  

import os
os.mkdir(r'C:\Users\MLoong\Desktop\Folder 88')

  Delete folder: rmdir (folder name)

Only empty directories can be deleted

import os
os.rmdir(r'C:\Users\MLoong\Desktop\Folder 88')

Delete non empty directory

import shutil
shutil.rmtree(r'C:\Users\MLoong\Desktop\folder')

  Get current program address: getcwd()

import os
print(os.getcwd())

Operation results

C:\Users\MLoong\PycharmProjects\pythonProject5

Path splicing: os.path.join(path1[, path2 [,...]]) combines multiple paths and returns  

import os
print(os.path)
print(os.path.join(os.getcwd(),'venv'))

  Operation results

C:\Users\MLoong\PycharmProjects\pythonProject5\venv

  Print file directory - first level file, no further in-depth search

import os
#Get a list of directories in python
listRS=os.listdir('d:/')
print(listRS)
#Returns a list of the names of all files in the computer's D disk
for item in listRS:
    print(item)
print('--------New method------------')
with os.scandir('d:/') as entries:
    for entry in entries:
        print(entry.name)

Print results  

['$RECYCLE.BIN', '360Downloads', '360 Speed browser download', 'BaiduNetdiskDownload']
$RECYCLE.BIN
360Downloads
360 Speed browser download
BaiduNetdiskDownload
--------New method------------
$RECYCLE.BIN
360Downloads
360 Speed browser download
BaiduNetdiskDownload

If only the file name is printed

print('--------Print out the file name------------')
basePath='d:/'
for entry in os.listdir(basePath):
    if os.path.isfile(os.path.join(basePath,entry)):
        print(entry)

 

7.time and datetime modules

Use of time module  

time.sleep(num) to pause the execution of the program. The unit of num is seconds  

import time
time.sleep(5) #Program pause for 5s

time.time() # returns the timestamp timestamp  

import time
print(time.time())  #Return timestamp 16362777714.4327455
print(time.ctime())  #String format sun Nov 7 17:35:14 2021
print(time.asctime())  #The return format is sun Nov 7 17:35:14 2021
print(time.localtime())  #Return to local time
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))

  Operation results

1636277924.8614864
Sun Nov  7 17:38:44 2021
Sun Nov  7 17:38:44 2021
time.struct_time(tm_year=2021, tm_mon=11, tm_mday=7, tm_hour=17, tm_min=38, tm_sec=44, tm_wday=6, tm_yday=311, tm_isdst=0)
2021-11-07 17:38:44

Use of time module  

Time formatted output  

%y two digit year representation (00-99)%Y four digit year (000-9999)%m month (01-12)
%Day of the month (0-31)%H 24-hour system hours (0-23)%I 12 hour system hours (01-12)
%M minutes (00 = 59)%S seconds (00-59)%a local simplified week name
%A local full week name%b local simplified month name%B local full month name
%c local corresponding date representation and time representation%One day in j years (001-366) 
%P equivalent of local A.M. or P.M%U number of weeks in a year (00-53) Sunday is the beginning of the week 
%w week (0-6), Sunday is the beginning of the week%W number of weeks in a year (00-53) Monday is the beginning of the week 
%X local corresponding date represents% x local corresponding time represents%Z the name%%% number of the current time zone itself 

  datetime module

The datetime module is mainly used for time calculation.

import datetime
print(datetime.datetime.now())
#Current time + 3 days
print(datetime.datetime.now()+datetime.timedelta(3))
#Current time + 3 hours
print(datetime.datetime.now()+datetime.timedelta(hours=3))
#Current time + 30 minutes
print(datetime.datetime.now()+datetime.timedelta(minutes=30))

Output results

2021-11-07 17:48:02.662048
2021-11-10 17:48:02.662048
2021-11-07 20:48:02.662048
2021-11-07 18:18:02.662048

8. Module production, release and installation

Definition of module: in python, a. py file is a module

Function: we can logically organize our python code and encapsulate functions in the form of library, which is very convenient for callers to use

You can define functions, classes, variables, and executable code

Note: different modules can define the same variable name, but the scope of variable name in each module is only in this module

Module classification:

Built in module   Custom module   Third party modules

Module fabrication

(1) Both and Python files can be used as a module. The name of the module is the name of the file. For example, create a test.py file, and create an add function in the file. Test.py is a module.

Create module

#Create a module called modeltest
def add(x,y):
    return x+y


#test
if __name__=='__main__': #The test results can not be displayed when calling
    res=add(2,5)
    print('Test module,%s'%res)

  Use module

import modeltest  #The first import method
#from modeltest import add  #The second import method
#from modeltest import *
res=modeltest.add(2,3)
print(res)

Module release

Usually use third party module is released by other developers, need to install after calling. Let's learn how to release a module.

(1) Put the written package into a modeltest / directory

(2) Create a file setup.py in the modeltest / directory

(3) , create module

(4) , generate compressed package

(5) View the structure under modeltest / directory from tree

Module installation

(1) Copy the compressed package generated in the previous section to the desktop and unzip it

tar -zxvf my_module-1.0.tar.gz

After decompression, a folder my_module-1.0 will be generated on the desktop

(2) . enter my_module-1.0 folder

        cd my_module-1.0

(3) . execute the command to install python setup.py install

(4) . check whether the installation is successful

         In the site packages directory under the python installation directory

(5) . the module is imported. If it can be imported, it indicates that the installation is successful

 

Posted by Sangre on Sun, 07 Nov 2021 13:06:13 -0800