In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to move files in batches with os module in python", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use os module to achieve batch mobile files in python" article.
Through the python operating system directory and its files, you need to use the os package. The following methods are used this time
Os.walk (), this method can traverse a folder and its subfiles (subfolders, etc.)
Os.rename (), this method is used to name a file or directory (because it is the absolute path to manipulate a file, it is actually equivalent to cutting a file)
You can also move or copy files with the help of the shutil library
Here is the sample code
Def run_main (source_dir, target_dir): num = 0 for root, dirs, files in os.walk (source_dir, topdown=False): # root indicates the folder path currently being accessed # dirs indicates the subdirectory name under this folder list # files indicates the file under this folder list for name in files: # print (os.path.join (root) Name) # print (type (os.path.join (root, name)) file = os.path.join (root, name) # the full path to the spliced file (note that we operate on a file or folder Be sure to use the absolute path) print (file) if file.split (".") [- 1] in ["xls", "xlsx" "csv"]: # use split to determine whether the obtained file path ends with csv # print (file) tar_file = file.split (".") [- 2] + str (num) + "." + file.split (".) [- 1] # in order to avoid duplicate files Add an incremental sequence number num to the original file name to form a new file name # print (target_dir+tar_file.split ("\") [- 1]) if os.path.isfile (target_dir+tar_file.split ("\") [- 1]): # determine whether the file print already exists in the target folder ("already saved") In this file ") else: print (" moving the {} file: {} ".format (num+1 Tar_file.split ("\\") [- 1]) os.rename (file, target_dir + tar_file.split ("\\") [- 1]) num + = 1if _ _ name__ ='_ _ main__': run_main ("D:/source/", "D:/target/")
For information about how to copy files, you can also use shutil
Copy the contents of the file to another file
Import shutilshutil.copyfileobj (open ('old.xml','r'), open (' new.xml','w')) shutil.copyfile (src, dst)
Copy a file
Shutil.copyfile ('f1.logbook no longer f2.log') # object file does not need to exist shutil.copymode (src, dst)
Copy permissions only. Content, groups and users remain the same.
Shutil.copymode ('f1.logbook, recording f2.log') # the object file must exist shutil.copystat (src, dst)
Copy only the status information, including: mode bits, atime, mtime, flags
Shutil.copystat ('f1.logbook, recording f2.log') # the object file must exist shutil.copy (src, dst)
Copy files and permissions
Import shutil23shutil.copy ('f1.logbook recording f2.log') shutil.copy2 (src, dst)
Copy files and status information
Import shutil23shutil.copy2 ('f1.logbook') shutil.ignore_patterns (* patterns) # basically does not use shutil.copytree (src, dst, symlinks=False, ignore=None)
Copy the folder recursively
Import shutilshutil.copytree ('folder1','folder2', ignore=shutil.ignore_patterns (' * .pyc', 'tmp*')) # destination directory cannot exist. Note that the parent directory of the folder2 directory must have writeable permission. Ignore means to exclude shutil.rmtree (path [, ignore_errors [, pyc]])
Delete files recursively
Import shutilshutil.rmtree ('folder1') shutil.move (src, dst)
To move files recursively, which is similar to the mv command, is actually renaming.
Import shutilshutil.move ('folder1','folder3') shutil.make_archive (base_name, format,...)
Create a zip file and return the file path, for example: zip, tar
Base_name: the file name of the compressed package, or the path of the compressed package. If it is just a file name, save it to the current directory, otherwise save to the specified path
For example: / tmp/data_bak = > Save to / tmp/
Format: compressed package type, "zip", "tar", "bztar", "gztar"
Root_dir: the folder path to be compressed (default current directory)
Owner: user, default current user
Group: group, default current group
Logger: used for logging, usually logging.Logger objects
# package the files under / data into the current program directory import shutilret = shutil.make_archive ("data_bak", 'gztar', root_dir='/data') # package the files under / data / tmp/ directory import shutilret = shutil.make_archive ("/ tmp/data_bak",' gztar', root_dir='/data') above is the content of this article on "how to use os module to move files in batch in python" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.