Python implements the method of copying multiple files to the same directory

  • 2020-05-12 02:47:59
  • OfStack

The example in this article shows how Python implements copying multiple files to the same directory. I will share it with you for your reference as follows:

There is a file with multiple file names, 1 file name and 1 line. If you want to copy these files to a directory, use the following code. The following code should be cross-system, except for the 1 sentence that separates the full path of the file. The following code assumes that all files are copied to the tmp subdirectory of the current directory, which needs to be created first.


#encoding=utf-8
import sys
import shutil
if len(sys.argv) < 2:
  print u' Missing parameter file name '
  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 sentence for windows
  dst = './tmp/' + lst[-1]
  print '==============================================================================='
  print src
  print '-------------------------------------------------------------------------------'
  print dst
  print '==============================================================================='
  shutil.copyfile(src, dst)
  idx += 1

The "src =" in line 19 was killed by the editor.

More about Python related topics: interested readers to view this site "Python file and directory skills summary", "Python skills summary text file", "Python URL skills summary", "Python pictures skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using skills summary", "Python string skills summary" and "Python introductory and advanced tutorial"

I hope this article has been helpful to you in Python programming.


Related articles: