下午好,複製文件 - 環蟒蛇
我這裏有一些代碼,是爲了從源文件夾複製約200 CSV年代進入基於「部門」,他們屬於目標文件夾。他們所屬的部門通過「符號列表」進行標識,其中包含csvs和一系列代號。該代碼適用於大部分部分,不同之處在於它將符號列表中最後csv中的代號列表複製並將所有這些CSV複製到所有目標文件夾中。基本上我需要以某種方式將第一個for循環與下面的3 for循環組合起來,但是我很難做到這一點。任何意見,不勝感激。
import os,sys,shutil
import glob
import pandas as pd
source_dir = 'C:\TS'
dest_dir = 'C:\TS\Combined\Groups\Cross Asset Class'
#dest_dir = 'C:\TS\Combined\copytest'
base = 'C:\TS\Combined\Groups'
dest_dirlist = (base +'/Cross Asset Class', base+'/Bonds', base + '/Commodities', \
base + '/Countries', base + '/Currencies', base + '/Industry Sectors', base + '/Segments and Styles', \
base + '/Us Sectors', base + '\Volatilities')
print(dest_dirlist)
symbolslist = (base+'/Cross Asset Class.csv', base+'/Bonds.csv',base+'/Commodities.csv' \
,base+'/Currencies.csv', base+'/Industry Sectors.csv', base+'/Segments and Styles.csv', \
base+'/US Sectors.csv')
for file in symbolslist:
print(file)
df_symbolslist = pd.read_csv(file)
print(df_symbolslist)
for ticker_file in glob.glob(os.path.join(source_dir, '*.csv*')):
for ticker in df_symbolslist['Ticker']:
print(ticker)
if ticker in ticker_file:
for path in dest_dirlist:
shutil.copy(ticker_file, path)
print(ticker + ' File Copied')
非常感謝您的時間。
'df_symbolslist = pd.read_csv(文件)'保持overwritting'df_symobolslist'。你只處理最後一個。我對這裏的格式感到困惑...... symbollist csv的外觀如何?他們如何編碼部門和符號? – tdelaney
這就是符號列表csv的外觀:http://prntscr.com/en97is – user7669093
這就是要複製的csvs的樣子。 http://prntscr.com/en98d2 – user7669093