0

I'm wanting to move .csv files after reading them.

The code I've come up with is to move any .csv files found in a folder, then direct to an archive folder.

src1 = "\\xxx\xxx\Source Folder"
dst1 = "\\xxx\xxx\Destination Folder"
for root, dirs, files in os.walk(src1):
    for f in files:
        if f.endswith('.csv'):
            shutil.move(os.path.join(root,f), dst1)

Note: I imported shutil at the beginning of my code.

Note 2: The destination archive folder is within the source folder - will this have implications for the above code?

When I run this, nothing happens. I get no error messages and the file remains in the source folder.

Any insight is appreciated.

Edit (some context on my goal): My overall code will be used to read .csv files that are moved manually into a source folder by users - I then want to archive these .csv files using Python once the data has been used. Every .csv file placed into the source folder by the users will have a different name - no .csv file name will be the same, which is why I want to search the source folder for .csv files and move them all.

BenjaminJ
  • 21
  • 3
  • Does it make a difference that the files are CSV files? In any case, it would help if you extracted a [mcve], because there are several places where the above code could fail. As a new user here, please also take the [tour] and read [ask]. – Ulrich Eckhardt Apr 14 '21 at 07:57

2 Answers2

1

After a bunch of research I have found a solution:

import shutil

source = r"\\xx\Source"
destination = r"\\xx\Destination"

files = os.listdir(source)

for file in files:
    new_path = shutil.move(f"{source}/{file}", destination)
    
print(new_path)

I was making it more complicated than it needed to be - because all files in the folder would be .csv anyway, I just needed to move all files. Thanks stackoverlfow.

BenjaminJ
  • 21
  • 3
0

You can use the pathlib module. I'm assuming you have got the same folder structure in the destination directory.

from pathlib import Path

src1 = "<Path to source folder>"
dst1 = "<Path to destination folder>"

for csv_file in Path(src1).glob('**/*.csv'):
    relative_file_path = csv_file.relative_to(src1)
    destination_path = dst1 / relative_file_path
    csv_file.rename(destination_path)

Explanation-

for csv_file in Path(src1).glob('**/*.csv'):

The glob(returns generator object) will capture all the CSV files in the directory as well as in the subdirectory. Now, we can iterate over the files one by one.

relative_file_path = csv_file.relative_to(src1)

All the csv_files are now pathlib path objects. So, we can use the functions that the library provides. One such function is relative to. Here It'll copy the relative path of the file from the src folder. Let's say you have a CSV file like-

scr_folder/A/B/c.csv - It'll copy A/B/c.csv

destination_path = dst1 / relative_file_path

As the folder structure is the same the destination path now becomes -

dst_folder/A/B/c.csv

csv_file.rename(destination_path)

At Last, rename will just move the file from src to destination.

Nk03
  • 14,699
  • 2
  • 8
  • 22