The following is a code to introduce to you the specified type files in the python package and compress the specified directory. The specific code is as follows:
import os import datetime import tarfile import fnmatch def find_spe_file(root, patterns=['*'], non_cludedir=[]): for root, dirnames, filenames in (root): for pattern in patterns: for filename in filenames: if (filename, pattern): #print(filename) yield (root, filename) def cre_tarfile(): args = ["*.jpg", "*.jepg"] now = ().strftime("%Y_%m_%d_%H_%M_%S") filename = "all_img_{0}.".format(now) with (filename, mode='w:gz') as f: for item in find_spe_file(".", args): #print(item) (item) if __name__ == "__main__": cre_tarfile()
Let's take a look at using python to read the specified type file in the specified directory
Preparation: Set the specified path and use the () method to obtain all files under the path
import os path = "d:\\data" # Set pathdirs = (path) # Get the file under the specified path
Loop judgment: Use the () method to filter out files of the specified type
for i in dirs: # Loop to read the file under the path and filter the output if (i)[1] == ".csv": # Filter csv files print i # Output allcsvdocument
Case display:
# encoding: utf-8 import os path = "d:\\data" # Set pathdirs = (path) # Get the file under the specified pathfor i in dirs: # Loop to read the file under the path and filter the output if (i)[1] == ".csv": # Filter csv files print i # Output allcsvdocument
Running results:
Function explanation:
(path)
Function function: Returns a list containing the name of the entry in the directory specified by the path. The list is in any order. It does not include special entries '.' and '..', even if they exist in the directory.
import os, sys path = "d:\\tmp\\" dirs = ( path ) for file in dirs: print (file)
Running results:
Java Multiple
Java Multiple Inheritance_files
ParallelPortViewer
(path)
Function function: Separate file name and extension; return (fname, fextension) tuple by default, and can be sliced
import os, sys path = 'c:\\csv\\' print (path)
Running results:
('c:\\csv\\test', '.csv')
Summarize
The above is the python package, compress and read the specified type of files in the specified directory introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!