1

I'm attempting too create a script in Python that reads through a text file. On each line of the text file, there's a file name. I want the script to cycle through each line of the text file and move the file with the file name from the current line it's cycled on, from it's source folder to a specific destination.

Hopefully this code gives a more accurate idea as to what I'm trying to do:

import shutil

dst = "C:\\Users\\Aydan\\Desktop\\1855"

with open('1855.txt') as my_file:
    for line in my_file:
        src = "C:\\Users\\Aydan\\Desktop\\data01\\BL\\ER\\D11\\fmp000005578\\" + line
        shutil.move(src, dst)

I was thinking of putting the contents of the file with the specific file names into an array, but I have 62700+ possible file names to end up with, so I was thinking if it just moved the files as it cycled onto every line that it would be a tad more efficient?

I also had the idea of using an iterator (or whatever you call it) setting i=[number of lines in the text file], then make it scroll that way, but seeing as if used for line in my_file: I thought it would make sense to use just line.

For a test, the text file contains:

BL_ER_D11_fmp000005578_0001_1.txt
BL_ER_D11_fmp000005578_0002_1.txt
BL_ER_D11_fmp000005578_0003_1.txt

The problem I'm having with this code is that it's not working as intended, I don't get any errors but the movement of files from one folder to another isn't happening. I would like it if you guys could possible point out a solution to this problem.

Thanks!

Aydan

Aydan Howell
  • 75
  • 1
  • 12

2 Answers2

1

I would try:

import os

dst = "C:\\Users\\Aydan\\Desktop\\1855\\" # make sure this is a path name and not a filename

with open('1855.txt') as my_file:
    for filename in my_file:
        src = os.path.join("C:\\Users\\Aydan\\Desktop\\data01\\BL\\ER\\D11\\fmp000005578\\", filename.strip() ) # .strip() to avoid un-wanted white spaces
        os.rename(src, os.path.join(dst, filename.strip()))
Shai
  • 111,146
  • 38
  • 238
  • 371
  • This didn't seem to work from me. My goal is to have the script concatenate the file name, on the line the loop is on in the text file, onto the end of the fmp000005578\\. is that what `filename.strip()` does? – Aydan Howell Jul 20 '17 at 11:14
  • `os.path.join()` suppose to add `filename` at the end of your source folder. `filename.strip()` is suppose to get rid of any un-wanted white spaces. – Shai Jul 20 '17 at 11:16
  • Huh, well it seems that the script doesn't seem to be working, as the files aren't being moved. – Aydan Howell Jul 20 '17 at 11:18
  • can you run the script in debug mode? can you open `'1855.txt'` file? is it possible this file is empty? what is `filename` in each iteration of the loop? how many iterations are executed? @AydanHowell – Shai Jul 20 '17 at 11:26
  • Ah hah! The piece of script takes into account the \n or line break, so it reads the filename as file.txt\n. Is there a way to get rid of the \n? This is the error I get : `Traceback (most recent call last): File "", line 4, in os.rename(src, os.path.join(dst, filename)) OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\Aydan\\Desktop\\data01\\BL\\ER\\D11\\fmp000005578\\BL_ER_D11_fmp000005578_0001_1.txt' -> 'C:\\Users\\Aydan\\Desktop\\1855\\BL_ER_D11_fmp000005578_0001_1.txt\n'` – Aydan Howell Jul 20 '17 at 11:31
  • the `.strip()` should handle this `'\n'`, you need another `.strip()` for the `dst` as well. Please see my edit. – Shai Jul 20 '17 at 11:33
  • @AydanHowell next time. before posting a question, take a deep breath, and make an effort to clearly write all the information in your head. This will (a) make your question better (b) result with faster and better answers for you. It's worth the effort. – Shai Jul 20 '17 at 11:55
  • 1
    It would be better in general to use `shutil.move`. Python 3.3+ on Windows doesn't support cross-volume moves with `os.rename`. – Eryk Sun Jul 20 '17 at 12:01
0

Using .strip() while providing destination path is solving this issue

import shutil
dst = r"C:/Users/Aydan/Desktop/1855/"

with open('test.txt') as my_file:
    for filename in my_file:
        file_name  = filename.strip()
        src = r'C:/Users/Aydan/Desktop/data01/BL/ER/D11/fmp000005578/'+ file_name    
        shutil.move(src, dst + file_name)
Suraj
  • 38
  • 7
  • This works the same as Shai's solution, although as he corrected on his code, file_name.strip() is needed to remove \n from the dst – Aydan Howell Jul 20 '17 at 11:52
  • Just coming to an understanding with this code, what does the r in the line `r"C:/Users/Aydan/Desktop/1855/"` before the directory stand for? – Aydan Howell Jul 20 '17 at 12:27
  • Adding "r" as Prefix for a string will treat the string as raw string. Which means all "\" or "/" will be treated as a string and will not be treated as escape characters. You can find more info here:https://stackoverflow.com/questions/33729045/what-does-an-r-represent-before-a-string-in-python – Suraj Aug 30 '17 at 06:08