程序在currrent目录中查找以“ Oddball_”开头并以“ .csv”结尾的文件,然后根据文件名通过编号为1到4的模块运行这些文件。
这对于根目录中的文件正确运行,但是在文件夹和子文件夹中进一步导航时会产生错误。
import sort_run1
import sort_run2
import sort_run3
import sort_run4
# Import the os module, for the os.walk function
import os
# Set the directory you want to start from
rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir):
for filename in fileList:
if filename.startswith('Oddball_') and filename.endswith('.csv'):
if dirName not in fileList:
print('directory location: ' + dirName)
if filename.startswith('Oddball_1'):
sort_run1.sort1(filename)
elif filename.startswith('Oddball_2'):
sort_run2.sort2(filename)
elif filename.startswith('Oddball_3'):
sort_run3.sort3(filename)
elif filename.startswith('Oddball_4'):
sort_run4.sort4(filename)
else:
continue
else:
continue
import pandas as pd
import pathlib
def sort1(pid):
print(pid)
# Name of file to read
filename = pid
# data frame to create from file
df = pd.read_csv(filename)
##
##I've removed all the stuff that happens here to minimize code
##
预期的输出将贯穿文件夹中的所有文件。
答案 0 :(得分:0)
在我的sort_run1.py文件中,
Mobile B
但需要以下
ABC App