使用不同目录中的文件的Python脚本

时间:2018-10-15 18:39:42

标签: python bash python-2.7 directory

我以以下格式保存了数据。

ID1/stats/file.stats
ID2/stats/file.stats

我有一个脚本,该脚本将把file.stats修改并转换为当前目录中的csv文件,但是我希望能够运行一次脚本并将文件夹ID1到IDn中的所有file.stats文件转换为IDn

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

    import os
    for root, subFolders, fileNames in os.walk("ID2/stats/"):
        for fileName in fileNames:
            with open(os.path.join(root, fileName), 'rb') as f:
                # Do Something with the files
                for character in f.read():
                    #reading character by character

希望这会有所帮助。

相关问题