删除 python 路径硬编码和传递列表
Removing python path hardcode and passing list
**更新 1/8/2019 0945 EST
我已经通过 bhakta0007 给出的函数传递了脚本,但收到路径错误 "The system cannot find the path specified:"。
经过审查,我将以下语句添加到脚本末尾以通过函数传递列表并且代码有效。
对于 fList 中的 f:
excel_csv(fList)
我已经添加了以下问题的答案。
我有一个小脚本,可以 运行 将 excel 文件转换为 .csv。目前,我必须使用硬编码的路径重复脚本。当前路径具有完全相同的结构,但 3 位标识符除外,我想创建一个我可以从中调用的列表。下面是我的代码。你会看到我有具有路径的变量,我将这些变量传递给 needed.I 已经研究过 os.path、glob 和 pathlib,但我找不到解决问题的好方法。
原码
import os
import glob
import pandas as pd
import shutil
Target_Path = os.path.join(os.path.dirname('//fs/Unprocessed/261/Edlog/Working/'))
Move_Path = os.path.join(os.path.dirname('//fs/Unprocessed/261/Edlog/ToProcess/'))
Process_Path = os.path.join(os.path.dirname('//fs/Unprocessed/261/Edlog/Processed/'))
os.chdir(Target_Path)
try:
for f in glob.glob('*.xls'):
out = f.split('.')[0]+'.csv'
df = pd.read_excel(f,)
df.to_csv(out, index=False)
finally:
for f in glob.glob('*.xlsx'):
out = f.split('.')[0]+'.csv'
df = pd.read_excel(f,)
df.to_csv(out, index=False)
xlsCounter = len(glob.glob1(Target_Path,"*.xls"))
xlsxCounter = len(glob.glob1(Target_Path,"*.xlsx"))
csvcounter = len(glob.glob1(Target_Path,"*.csv"))
if csvcounter == xlsCounter + xlsxCounter :
print('Complete Convert')
else:
print('Failed Convert')
for files in glob.glob('*.csv'):
shutil.move(files, Move_Path)
for files in glob.glob('*.xls'):
shutil.move(files, Process_Path)
for files in glob.glob('*.xlsx'):
shutil.move(files, Process_Path)
if len(os.listdir(Target_Path) ) == 0:
print('Complete Move')
else:
print('Failed Move')
import os
import glob
import pandas as pd
import shutil
def process(folders):
for f in folders:
Target_Path = os.path.join(os.path.dirname('//fs/Unprocessed/{}/Edlog/Working/').format(folder))
Move_Path = os.path.join(os.path.dirname('//fs/Unprocessed/{}/Edlog/ToProcess/').format(folder))
Process_Path = os.path.join(os.path.dirname('//fs/Unprocessed/{}/Edlog/Processed/').format(folder))
os.chdir(Target_Path)
<Rest of our code>
fList = [261, 262, 278, 300]
process(fList)
我使用了从 Bhakta0007 创建的函数,但收到 "The system cannot find the path specified:" 错误。
-添加了修订-
我在脚本末尾添加了一个 "For" 子句,并通过该函数传递了列表,并且能够在所有目录中成功 运行 脚本。
我还为 "Facility" 使用了 fstring 而不是 .format(facility)
下面是工作代码
import os
import glob
import pandas as pd
import shutil
def excel_csv(facility):
for f in facility:
Target_Path = os.path.join(os.path.dirname(f'//fs/Unprocessed/{facility}/Edlog/Working/'))
Move_Path = os.path.join(os.path.dirname(f'//fs/Unprocessed/{facility}/Edlog/ToProcess/'))
Process_Path = os.path.join(os.path.dirname(f'//fs/Unprocessed/{facility}/Edlog/Processed/'))
os.chdir(Target_Path)
try:
for f in glob.glob('*.xls'):
out = f.split('.')[0]+'.csv'
df = pd.read_excel(f,)
df.to_csv(out, index=False)
finally:
for f in glob.glob('*.xlsx'):
out = f.split('.')[0]+'.csv'
df = pd.read_excel(f,)
df.to_csv(out, index=False)
xlsCounter = len(glob.glob1(Target_Path,"*.xls"))
xlsxCounter = len(glob.glob1(Target_Path,"*.xlsx"))
csvcounter = len(glob.glob1(Target_Path,"*.csv"))
if csvcounter == xlsCounter + xlsxCounter :
print('Complete Convert')
else:
print('Failed Convert')
for files in glob.glob('*.csv'):
shutil.move(files, Move_Path)
for files in glob.glob('*.xls'):
shutil.move(files, Process_Path)
for files in glob.glob('*.xlsx'):
shutil.move(files, Process_Path)
if len(os.listdir(Target_Path) ) == 0:
print('Complete Move')
else:
print('Failed Move')
fList = ['261', '262', '278', '300']
for f in fList:
excel_csv(fList)
**更新 1/8/2019 0945 EST
我已经通过 bhakta0007 给出的函数传递了脚本,但收到路径错误 "The system cannot find the path specified:"。
经过审查,我将以下语句添加到脚本末尾以通过函数传递列表并且代码有效。
对于 fList 中的 f: excel_csv(fList)
我已经添加了以下问题的答案。
我有一个小脚本,可以 运行 将 excel 文件转换为 .csv。目前,我必须使用硬编码的路径重复脚本。当前路径具有完全相同的结构,但 3 位标识符除外,我想创建一个我可以从中调用的列表。下面是我的代码。你会看到我有具有路径的变量,我将这些变量传递给 needed.I 已经研究过 os.path、glob 和 pathlib,但我找不到解决问题的好方法。
原码
import os
import glob
import pandas as pd
import shutil
Target_Path = os.path.join(os.path.dirname('//fs/Unprocessed/261/Edlog/Working/'))
Move_Path = os.path.join(os.path.dirname('//fs/Unprocessed/261/Edlog/ToProcess/'))
Process_Path = os.path.join(os.path.dirname('//fs/Unprocessed/261/Edlog/Processed/'))
os.chdir(Target_Path)
try:
for f in glob.glob('*.xls'):
out = f.split('.')[0]+'.csv'
df = pd.read_excel(f,)
df.to_csv(out, index=False)
finally:
for f in glob.glob('*.xlsx'):
out = f.split('.')[0]+'.csv'
df = pd.read_excel(f,)
df.to_csv(out, index=False)
xlsCounter = len(glob.glob1(Target_Path,"*.xls"))
xlsxCounter = len(glob.glob1(Target_Path,"*.xlsx"))
csvcounter = len(glob.glob1(Target_Path,"*.csv"))
if csvcounter == xlsCounter + xlsxCounter :
print('Complete Convert')
else:
print('Failed Convert')
for files in glob.glob('*.csv'):
shutil.move(files, Move_Path)
for files in glob.glob('*.xls'):
shutil.move(files, Process_Path)
for files in glob.glob('*.xlsx'):
shutil.move(files, Process_Path)
if len(os.listdir(Target_Path) ) == 0:
print('Complete Move')
else:
print('Failed Move')
import os
import glob
import pandas as pd
import shutil
def process(folders):
for f in folders:
Target_Path = os.path.join(os.path.dirname('//fs/Unprocessed/{}/Edlog/Working/').format(folder))
Move_Path = os.path.join(os.path.dirname('//fs/Unprocessed/{}/Edlog/ToProcess/').format(folder))
Process_Path = os.path.join(os.path.dirname('//fs/Unprocessed/{}/Edlog/Processed/').format(folder))
os.chdir(Target_Path)
<Rest of our code>
fList = [261, 262, 278, 300]
process(fList)
我使用了从 Bhakta0007 创建的函数,但收到 "The system cannot find the path specified:" 错误。
-添加了修订-
我在脚本末尾添加了一个 "For" 子句,并通过该函数传递了列表,并且能够在所有目录中成功 运行 脚本。
我还为 "Facility" 使用了 fstring 而不是 .format(facility)
下面是工作代码
import os
import glob
import pandas as pd
import shutil
def excel_csv(facility):
for f in facility:
Target_Path = os.path.join(os.path.dirname(f'//fs/Unprocessed/{facility}/Edlog/Working/'))
Move_Path = os.path.join(os.path.dirname(f'//fs/Unprocessed/{facility}/Edlog/ToProcess/'))
Process_Path = os.path.join(os.path.dirname(f'//fs/Unprocessed/{facility}/Edlog/Processed/'))
os.chdir(Target_Path)
try:
for f in glob.glob('*.xls'):
out = f.split('.')[0]+'.csv'
df = pd.read_excel(f,)
df.to_csv(out, index=False)
finally:
for f in glob.glob('*.xlsx'):
out = f.split('.')[0]+'.csv'
df = pd.read_excel(f,)
df.to_csv(out, index=False)
xlsCounter = len(glob.glob1(Target_Path,"*.xls"))
xlsxCounter = len(glob.glob1(Target_Path,"*.xlsx"))
csvcounter = len(glob.glob1(Target_Path,"*.csv"))
if csvcounter == xlsCounter + xlsxCounter :
print('Complete Convert')
else:
print('Failed Convert')
for files in glob.glob('*.csv'):
shutil.move(files, Move_Path)
for files in glob.glob('*.xls'):
shutil.move(files, Process_Path)
for files in glob.glob('*.xlsx'):
shutil.move(files, Process_Path)
if len(os.listdir(Target_Path) ) == 0:
print('Complete Move')
else:
print('Failed Move')
fList = ['261', '262', '278', '300']
for f in fList:
excel_csv(fList)