Detail Python text manipulation related modules

  • 2020-06-07 04:46:24
  • OfStack

Detail Python text manipulation related modules

linecache - Tries to optimize by using cache internals to efficiently read any line from any file.

Main Methods:


linecache.getline(filename, lineno[, module_globals]): Gets the contents of the specified row  
linecache.clearcache() : Clear the cache  
linecache.checkcache([filename]): Check the validity of the cache 

dircache - Defines a function that USES the cache to read the directory list and mtime of the directory to invalidate the cache. Methods for annotating directories are also defined.

Main Methods:


dircache.reset(): Reset the directory cache.  
dircache.listdir(path): return path List of directories. unless path Change, otherwise calling the method again will repeat reading the directory structure.  
dircache.opendir(path): and listdir Same function. It exists for backward compatibility.  
dircache.annotate(head,list): Assuming that list Is relative to head ", then add "in the appropriate place to each path of the directory". / "  

filecmp - Defines a function for comparing files and directories.

Main Methods:


filecmp.cmp(f1, f2[, shallow]) : Compare two files that are not equal  
filecmp.cmpfiles(dir1, dir2, common[, shallow]) : Compare two directories under the same file name of the file is not equal.  

Class: class ES30en. dircmp(a, b[, ignore[, hide]]) : Build a new directory comparison object to compare a and b.

fileinput -- Implements helper classes and methods to help you quickly manipulate standard input or 1 string of text.

Main Methods:


fileinput.input([files[, inplace[, backup[, bufsize[, mode[, openhook]]]]]]) : create 1 a  FileInput Entity.  
fileinput.filename() : Returns the filename currently read  
fileinput.lineno() : Returns the number of rows read cumulatively  
fileinput.nextfile() : Closes the current row and the next iteration skips to read 1 Of the file 1 Line.  
 Class:  class fileinput.FileInput([files[, inplace[, backup[, bufsize[, mode[, openhook]]]]]]) 

os.path - includes actions on paths and file properties.

Main Methods:


os.path.abspath(path): return path Absolute path of  
os.path.dirname(path) Returns the path Directory name of  
os.path.exists(path) : Determine if the path exists  
os.path.getatime(path) Returns the path Time of last visit  
os.path.getmtime(path) Returns the path Last modified time 

shutil - Provides advanced operations on files and filesets.

Main Methods:


shutil.copyfileobj(fsrc, fdst[, length]) : To the class file object fsrc Copy the contents to the class file object fdst 
shutil.copyfile(src, dst): The file src The contents of the (excluding metadata) are copied to the file dst 
shutil.copymode(src, dst): will src Permissions copy to dst 
shutil.copystat(src, dst): Copy permission bits, last access time, last modification time, and flags .  
shutil.copy(src, dst) : Copy the file src To a file or directory dst . The permission bits are also copied.  
shutil.copy2(src, dst): and copy() The difference is that the metadata is also copied. Similar to copy(), again copystat() 
shutil.rmtree(path[, ignore_errors[, onerror]]) : Removes the entire directory tree  
shutil.move(src, dst): Move a file or path recursively  

In addition to working with 1 general files, you can also work with archive files


shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]]): Create the archive file.  
tempfile -   Generate temporary files and directories  

Main Methods:


tempfile.TemporaryFile([mode='w+b'[, bufsize=-1[, suffix= " [, prefix='tmp'[, dir=None]]]]]) Returns the 1 A class file object that can be used as a temporary storage area.  
tempfile.mkstemp([suffix= " [, prefix='tmp'[, dir=None[, text=False]]]]) : Created in the safest possible way 1 A temporary file.  

stat -- Returns system status information for the file, etc.

struct -- Operation of base 2 files. This includes conversions between base 2 data types and str.

In addition, there are other modules that specialize in operating on specific types of files (class files). For example: lxml, CSV, zipfile, tarfile, etc.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: