相关疑难解决方法(0)

使用Glob()在Python中递归查找文件?

这就是我所拥有的:

glob(os.path.join('src','*.c'))
Run Code Online (Sandbox Code Playgroud)

但我想搜索src的子文件夹.像这样的东西会起作用:

glob(os.path.join('src','*.c'))
glob(os.path.join('src','*','*.c'))
glob(os.path.join('src','*','*','*.c'))
glob(os.path.join('src','*','*','*','*.c'))
Run Code Online (Sandbox Code Playgroud)

但这显然是有限和笨重的.

python filesystems glob path fnmatch

676
推荐指数
15
解决办法
59万
查看次数

读取所有目录中的所有文件

我有代码工作来读取单个文本文件的值,但是很难从所有目录中读取所有文件并将所有内容放在一起.

这是我有的:

filename = '*'
filesuffix = '*'
location = os.path.join('Test', filename + "." + filesuffix)
Document = filename
thedictionary = {}
with open(location) as f:
 file_contents = f.read().lower().split(' ') # split line on spaces to make a list
 for position, item in enumerate(file_contents): 
     if item in thedictionary:
      thedictionary[item].append(position)
     else:
      thedictionary[item] = [position]
wordlist = (thedictionary, Document)
#print wordlist
#print thedictionary
Run Code Online (Sandbox Code Playgroud)

请注意,我正在尝试将通配符*插入文件名以及文件后缀的通配符.我收到以下错误:

"IOError:[Errno 2]没有这样的文件或目录:'Test/. '"

我不确定这是否是正确的方法,但似乎如果我以某种方式让通配符工作 - 它应该工作.

我已经得到了这个例子: Python - 从子目录中找不到目录文件中的文件(在那里)

这有点不同 - 但不知道如何更新它来读取所有文件.我在想这个初始代码集:

previous_dir = os.getcwd()
os.chdir('testfilefolder') …
Run Code Online (Sandbox Code Playgroud)

python directory-structure file file-handling

9
推荐指数
1
解决办法
2万
查看次数