目前,我有一个文件夹,其中包含运行一些代码并转换为csv所需的各种文件。我正在尝试查看是否可以检查文件夹中文件的名称,然后执行相应的代码。我试过了:
for xlsb_file in pathlib.Path('Directory Path').glob('Filename*.xlsb'):
RUN CODE
convert dataset into a csv
由于某种原因,该代码似乎可以正确执行,但是没有输出csv文件。理想情况下,当我运行代码时,我想让代码检测文件名,运行相应的代码,然后吐出一个csv。
反正有这样做吗?感谢所有帮助!
你可以试试:
import os
def convert_to_csv(filename):
"""this function converts and save a csv version of the file"""
def convert_this_file(filename):
"""this functions checks if a file should be converted"""
return filename.endswith('.xlsb')
for root, folders, files in os.walk('Directory Path'):
for file in files:
filename = os.path.join(root, file)
if convert_this_file(filename):
convert_to_csv(filename)
break
如果您要代码递归扫描子目录,请删除“中断”
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句