【问题标题】:Python - loop through a directory and set a variable to a filename to be manipulated [duplicate]Python - 遍历目录并将变量设置为要操作的文件名[重复]
【发布时间】:2015-04-21 20:25:45
【问题描述】:

我正在尝试将目录和文件名循环到要操作的变量中。

例子:

目录包含:

stuff.txt 主页.txt ... 规格.txt

for loop:
     var = [filenames in the dir such as stuff,home,spec..]
     print hash(var)

是否可以包含子目录?

【问题讨论】:

标签: python


【解决方案1】:

您可以使用os.listdir 来开始这样的事情:

import os
your_dir = '/home/...' # Dir path

for filename in os.listdir(your_dir):
    if os.path.isfile(filename):
        print filename
        do_something(filename)

do_something 是你的哈希函数等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 2011-12-29
    • 2017-02-04
    • 2021-10-24
    • 2014-02-05
    • 2018-04-07
    • 2021-12-28
    相关资源
    最近更新 更多