English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes how to implement the method of copying multiple files to the same directory in Python. Share it with everyone for reference, as follows:
There is a file that contains multiple filenames, one per line. If you want to copy these files to a directory, you can use the following code. The following code is cross-platform, except for the line that separates the full path of the file. The following code assumes that all files are copied to the current directory's tmp subdirectory, which must be created first.
#encoding=utf-8 import sys import shutil if len(sys.argv) < 2: print u'缺少参数文件名' exit(-1) par_file = sys.argv[1] file_lst = [] try: with open(par_file, 'r') as file: for line in file: file_lst.append(line) except IOError as err: print('File error: ' + str(err)) exit(-1) idx = 1 for item in file_lst: print idx item.strip() lst = src.split('\\') # This line is for Windows dst = '.'/tmp/' + lst[-1] print '===============================================================================' print src print '"-------------------------------------------------------------------------------' print dst print '===============================================================================' shutil.copyfile(src, dst) idx += 1
The19The 'src = ' line has been deleted by the editor.
Readers who are interested in more content related to Python can check the special topics on this site: 'Summary of Python File and Directory Operation Skills', 'Summary of Python Text File Operation Skills', 'Summary of Python URL Operation Skills', 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', and 'Classic Tutorial of Python入门与进阶'.
I hope the content described in this article is helpful to everyone's Python program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any infringement, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.