current position:Home>Python file and folder operations

Python file and folder operations

2022-05-15 05:50:53Tang Keke, Laoxiang, Henan

os modular

  • chdir(path) hold path Set as current working directory
  • curdir Return to the current folder
  • environ A dictionary containing system environment variables and values
  • extsep The file extension separator used by the current operating system
  • get_exec_path() Return the search path of executable file
  • getcwd() Return to the current working directory
  • listdir(path) return path List of files and directories under the directory
  • remove(path) Delete the specified file , Require users to have permission to delete files , And the file has no read-only or other special properties
  • rename(src, dst) Rename a file or directory , You can move files , If the target file already exists, an exception is thrown , Cannot span disks or partitions
  • replace(old, new) Rename a file or directory , If the target file already exists, directly overwrite , Cannot span disks or partitions
  • scandir(path=‘.’) Returns the file containing all the files in the specified folder DirEntry Object's iteration object , Folder traversal time ratio listdir() More efficient
  • sep The path separator used by the current operating system
  • startfile(filepath [, operation]) Use the associated application to open the specified file or start the specified application
  • system() Start an external program
>>> import os
>>> import os.path
>>> os.rename('C:\\dfg.txt',
'D:\\test2.txt') #rename() You can rename and move files 
>>> [fname for fname in os.listdir('.')if fname.endswith(('.pyc','.py','.pyw'))] # The result is a little 
>>> os.getcwd() # Return to the current working directory 'C:\\Python35'
>>> os.mkdir(os.getcwd()+'\\temp') # Create directory 
>>> os.makedirs(os.getcwd()+'\\temp', exist_ok=True) 
# You can recursively create 
>>> os.chdir(os.getcwd()+'\\temp') # Change the current working directory 
>>> os.getcwd()
'C:\\Python35\\temp'
>>> os.mkdir(os.getcwd()+'\\test')
>>> os.listdir('.')
['test']
>>> os.rmdir('test') # Delete directory 
>>> os.listdir('.')
[]
>>> os.environ.get('path') # Get system variables path Value 
>>> import time
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.stat('yilaizhuru2.py').st_ctime))# See when the file was created 
'2016-10-18 15:58:57'
>>> os.startfile('notepad.exe') # Start Notepad program 

os.path modular

  • abspath(path) Returns the absolute path of the given path
  • basename(path) Returns the last component of the specified path
  • commonpath(paths) Returns the longest common path of a given multiple paths
  • commonprefix(paths) Returns the longest common prefix of a given multiple paths
  • dirname§ Returns the folder portion of the given path
  • exists(path) Judge whether the file exists
  • getatime(filename) Returns the last access time of the file
  • getctime(filename) Returns the creation time of the file
  • getmtime(filename) Returns the last modification time of the file
  • getsize(filename) Returns the size of the file
  • isabs(path) Judge path Is it an absolute path
  • isdir(path) Judge path Is it a folder
  • isfile(path) Judge path Is it a document
  • *join(path, paths) Connect two or more path
  • realpath(path) Returns the absolute path of the given path
  • relpath(path) Returns the relative path of the given path , Cannot span disk drives or partitions
  • samefile(f1, f2) test f1 and f2 Whether these two paths refer to the same file
  • split(path) Take the last slash in the path as the separator to separate the path into two parts , Returns... As a tuple
  • splitext(path) Separate file extensions from paths
  • splitdrive(path) Separate drive names from paths
>>> path=
'D:\\mypython_exp\\new_test.txt'
>>> os.path.dirname(path) # Return the folder name of the path 'D:\\mypython_exp'
>>> os.path.basename(path) # The last component of the return path 'new_test.txt'
>>> os.path.split(path) # Split file path and file name ('D:\\mypython_exp','new_test.txt')
>>> os.path.split('') # The segmentation result is an empty string 
('','')
>>> os.path.split('C:\\windows') # Use the last slash as the separator 
('C:\\','windows')
>>> os.path.split('C:\\windows\\')
('C:\\windows','')
>>> os.path.splitdrive(path) # Split drive symbols 
('D:','\\mypython_exp\\new_test.txt')
>>> os.path.splitext(path) # Shard file extension 
('D:\\mypython_exp\\new_test',='.txt')

Use the recursive method to traverse all subdirectories and files under the specified directory .

from os import listdir
from os.path import join, isfile, isdir
def listDirDepthFirst(directory):
''' Depth first traversal of folders '''
# Traversal folder , If it is a file, output it directly 
# If it's a folder , The output shows , Then recursively traverse the folder 
for subPath in listdir(directory):
    path = join(directory, subPath)
    if isfile(path):
        print(path)
    elif isdir(path):
        print(path)
        listDirDepthFirst(path)

shutil modular

  • copy(src, dst) Copy file , The new file has the same file attributes , Throw an exception if the target file already exists
  • copy2(src, dst) Copy file , The new file has exactly the same properties as the original file , Including creation time 、 Modification time and last access time, etc , Throw an exception if the target file already exists
  • copyfile(src, dst) Copy file , Do not copy file properties , If the target file already exists, directly overwrite
  • copyfileobj(fsrc, fdst) Copy data between two file objects , for example
    copyfileobj(open('123.txt'),open('456.txt','a'))
    
  • copymode(src, dst) hold src Mode bit of (mode bit) Copied to the dst On , The latter two have the same pattern
  • copystat(src, dst) hold src Mode bit of 、 All states such as access time are copied to dst On
  • copytree(src, dst) Recursively copy folders
  • disk_usage(path) Check disk usage
  • move(src, dst) Move files or move folders recursively , You can also rename files and folders
  • rmtree(path) Recursively delete folders
  • make_archive(base_name,format, root_dir=None,base_dir=None) establish tar or zip Format of compressed files
  • unpack_archive(filename,extract_dir=None,format=None) Extract the compressed file

Use case

How to use the standard library shutil Of copyfile() Method to copy the file

>>> import shutil # Import shutil modular 
>>> shutil.copyfile('C:\\dir.txt','C:\\dir1.txt') # Copy file 

take C:\Python35\Dlls Folder and all files in the folder are compressed to D:\a.zip file : take C:\Python35\Dlls Folder and all files in the folder are compressed to D:\a.zip file :

>>> shutil.make_archive('D:\\a','zip','C:\\Python35',
'Dlls')
'D:\\a.zip'

The file just compressed D:\a.zip Unzip to D:\a_unpack Folder :

>>> shutil.unpack_archive('D:\\a.zip','D:\\a_unpack')

The following code uses shutil Module method to delete the folder just extracted

>>> shutil.rmtree('D:\\a_unpack')

Code use shutil Of copytree() Function recursively copies a folder , And ignore the extension pyc Documents and “ new ” Files and subfolders beginning with the word :

>>> from shutil import copytree, ignore_patterns
>>> copytree('C:\\python35\\test','D:\\des_test'ignore=ignore_patterns('*.pyc',' new *'))

practice

Batch randomize all file names in the specified folder , Keep the file type unchanged .

from string import ascii_letters
from os import listdir, rename
from os.path import splitext, join
from random import choice, randint
def randomFilename(directory):
for fn in listdir(directory):
    # segmentation , Get the file name and extension 
    name, ext = splitext(fn)
    n = randint(5, 20)
    # Generate a random string as the new file name 
    newName = ''.join((choice(ascii_letters) for i in range(n)))
    # Change file name 
    rename(join(directory, fn), join(directory, newName+ext))
randomFilename('C:\\test')

Programming , Count the size of the specified folder and the number of files and subfolders .

import os
totalSize = 0
fileNum = 0
dirNum = 0
def visitDir(path):
global totalSize
    global fileNum
    global dirNum
    for lists in os.listdir(path):
        sub_path = os.path.join(path, lists)
        if os.path.isfile(sub_path):
            fileNum = fileNum+1 # Count the number of documents 
            totalSize = totalSize+os.path.getsize(sub_path) # Count the total size of the file 
        elif os.path.isdir(sub_path):
            dirNum = dirNum+1 # Count the number of folders 
visitDir(sub_path) # Recursively traverse subfolders 

def main(path):
    if not os.path.isdir(path):
        print('Error:"', path,'" is not a directory or does notexist.')
        return
    visitDir(path)
def sizeConvert(size): # Unit conversion 
    K, M, G = 1024, 1024**2, 1024**3
    if size >= G:
        return str(size/G)+'G Bytes'
    elif size >= M:
        return str(size/M)+'M Bytes'
    elif size >= K:
        return str(size/K)+'K Bytes'
    else:
        return str(size)+'Bytes'

def output(path):
    print('The total size of '+path+' is:'+sizeConvert(totalSize)+'('+str(totalSize)+' Bytes)')
    print('The total number of files in '+path+' is:',fileNum)
    print('The total number of directories in '+path+' is:',dirNum)
if __name__=='__main__':
    path = 'C:\\text_dir'
    main(path)
    output(path)

Programming , Recursively delete files of the specified type and size in the specified folder 0 The file of .

from os.path import isdir, join, splitext
from os import remove, listdir, chmod, stat
filetypes = ('.tmp', '.log', '.obj', '.txt') # Specify the file type to delete 
def delCertainFiles(directory):
    if not isdir(directory):
        return
    for filename in listdir(directory):
        temp = join(directory, filename)
        if isdir(temp):
            delCertainFiles(temp) # Recursively call 
        elif splitext(temp)[1] in filetypes or stat(temp).st_size==0:
            chmod(temp, 0o777) # Modify file properties , Get delete permission 
            remove(temp) # Delete file 
            print(temp, ' deleted....')
delCertainFiles(r'C:\test')

copyright notice
author[Tang Keke, Laoxiang, Henan],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110611060134.html

Random recommended