0

**更新 2019 年 1 月 8 日 0945 EST

我已通过 bhakta0007 提供的函数传递脚本,但收到路径错误“系统找不到指定的路径:”。

经过审查,我在脚本末尾添加了以下语句,以通过函数传递列表并且代码有效。

对于 fList 中的 f:excel_csv(fList)

我已经为下面的问题添加了答案。

我有一个小脚本,我运行它来将 excel 文件转换为 .csv。目前,我必须使用硬编码的路径重复脚本。当前路径具有完全相同的结构,但我想创建一个可以从中调用的列表的 3 位标识符除外。下面是我的代码。您会看到我有具有路径的变量,并且我在需要的地方传递了这些变量。我已经研究了 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')
4

2 回答 2

0

我使用了从 Bhakta0007 创建的函数,但收到“系统找不到指定的路径:”错误。

-添加了修订-

我在脚本末尾添加了一个“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)
于 2019-01-08T15:46:53.630 回答
-1
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)
于 2019-01-08T03:04:34.307 回答