Python copy or move files whose names are on a list in a text file
Asked Answered
A

3

6

Can anyone tell me how to copy or move a batch of files to another directory or other directories. The name of the files are in a list that are in a text file. I'm working on a Windows system. The text file contains a list similar to this:

C:\dir1\dir3\dir4\file1.pdf
C:\dir5\dir6\file2.txt
c:\dir7\dir8\dir9\dir10\file3.pdf

...more file names

I've tried readline() to read the file list and shutil.move(src, dest) to move the files, but don't know how to pass the src file properly, without getting an error. Any suggestions on this way or another would be appreciated? Thanks.

I tested using a file list that had just one entry: (filetest.txt): C:\Documents and Settings\Owner\My Documents\movetest.txt

import shutil

# shutil.move(r'C:\Documents and Settings\Owner\My Documents\test4.txt', r'C:\Documents and Settings\Owner\My Documents\Test\test4.txt')

filein = open('filetest.txt', 'r')
line = filein.readline()
name = 'r' + "'" + line[:len(line) - 1] + "'"
shutil.move(name, 'movetest.txt')
filein.close()`

Traceback:

Traceback (most recent call last):
  File "C:\Python33\Lib\shutil.py", line 522, in move
    os.rename(src, real_dst)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'movetest.txt'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Documents and Settings/Owner/My Documents/Python 3 Programs/MoveTest10.py", line 12, in <module>
    shutil.move(name, 'movetest.txt')
  File "C:\Python33\Lib\shutil.py", line 534, in move
    copy2(src, real_dst)
  File "C:\Python33\Lib\shutil.py", line 243, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Python33\Lib\shutil.py", line 109, in copyfile
    with open(src, 'rb') as fsrc:
OSError: [Errno 22] Invalid argument: "r'C:\\Documents and Settings\\Owner\\My Documents\\movetest.txt'"
Aton answered 24/2, 2014 at 13:23 Comment(0)
B
1

you have to put r"c:\test" in a python program just because it is how the literal C:\test is denoted in python. When they are read in a file each character represent itself so just

import shutil

filein = open('filetest.txt', 'r')
line = filein.readline()
name = line[:len(line) - 1]
shutil.move(name, 'movetest.txt')
filein.close()`

should work

Build answered 24/2, 2014 at 14:6 Comment(0)
C
1

This is what I think may work:

# #If the string matches a file name move it to a new directory
dst = #Where you're taking from

with open('XXX.txt') as my_file:
     for filename in my_file:
     src = os.path.join(#Where its going to, filename.strip() ) 
     #shutil.copy(src, os.path.join(dst, filename.strip()))
     shutil.copy(os.path.join(src, filename), os.path.join(dst, filename))
Consideration answered 5/7, 2022 at 20:20 Comment(0)
C
0

You need to escape the backslashes, so python understands your paths: Instead of:

C:\dir1\dir3\dir4\file1.pdf

Use:

C:\\dir1\\dir3\\dir4\\file1.pdf

When you read the the file you can do:

for line in file:
    line = line.replace('\\', '\\\\')

For example:

In [5]: path='c:\\dir\\file'

In [6]: path.replace('\\','\\\\')
Out[6]: 'c:\\\\dir\\\\file'
Cardiganshire answered 24/2, 2014 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.