Skip to content Skip to sidebar Skip to footer

How To Move Files Listed In A Text File From One Folder To Another In Python

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

Solution 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()))

Solution 2:

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

import shutil
dst = r"C:/Users/Aydan/Desktop/1855/"withopen('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)

Post a Comment for "How To Move Files Listed In A Text File From One Folder To Another In Python"