Reputation: 1
I am new to Python, hope someone can help me with this question that I am currently stuck at. What I am trying to do is to move the files under a source folder to a destination folder after I read the data and combine them into one data frame. My script is attached here:
import os
import pandas as pd
import re
import psycopg2 as ps
import shutil
sub_folder = 'team project'
current_folder = os.getcwd()
working_folder = os.path.join(current_folder, sub_folder)
files = os.listdir(working_folder)
batch_files = [f for f in files if 'April 16' in f or 'April 23' in f]
batch_files = [ os.path.join(working_folder, f) for f in batch_files]
df = pd.DataFrame()
for f in batch_files:
data = pd.read_excel(f, 'Sheet1')
df = df.append(data)
df
des_folder = 'destination'
destination = os.path.join(current_folder, des_folder)
dest = os.fspath(destination)
srt = os.fspath(batch_files)
dest = shutil.move(srt, dest)
when I try to run: srt = os.fspath(batch_files), I encountered the error message: TypeError: expected str, bytes or os.PathLike object, not list
Could anyone kindly provide a solution to this? Appreciate it
Upvotes: 0
Views: 7099
Reputation: 3465
As both folder team_project
and destination
are directly below the current folder I would refactor (rewrite) your code as follows:
import os
import shutil
import pandas as pd
current_folder = os.path.join(os.getcwd())
working_folder = 'team_project'
destination_folder = 'destination'
batch_files = [
os.path.join(working_folder, f) for f in os.listdir(working_folder)
if any(w in f for w in ['April 16', 'April 23'])
]
df = pd.DataFrame()
for f in batch_files:
data = pd.read_excel(f, 'Sheet1')
df = df.append(data)
shutil.move(f, destination_folder)
print(df.head())
Upvotes: 0
Reputation: 5648
do you want this?
for f in batch_files:
data = pd.read_excel(f, 'Sheet1')
df = df.append(data)
#move everything below here into your for loop
df
des_folder = 'destination'
destination = os.path.join(current_folder, des_folder)
dest = os.fspath(destination)
srt = os.fspath(f) # remove the list and use 'f'
dest = shutil.move(srt, dest)
Upvotes: 1