小编gre*_*reg的帖子

按文件名通配符打开文件

我有一个文本文件目录,都有扩展名.txt.我的目标是打印文本文件的内容.我希望能够使用通配符*.txt来指定我想要打开的文件名(我正在考虑类似的行F:\text\*.txt?),拆分文本文件的行,然后打印输出.

这是我想要做的一个例子,但我希望能够somefile在执行命令时进行更改.

f = open('F:\text\somefile.txt', 'r')
for line in f:
    print line,
Run Code Online (Sandbox Code Playgroud)

我之前检查过glob模块,但我无法弄清楚如何对文件做任何实际操作.这是我想出来的,而不是工作.

filepath = "F:\irc\as\*.txt"
txt = glob.glob(filepath)

lines = string.split(txt, '\n') #AttributeError: 'list' object has no attribute 'split'
print lines
Run Code Online (Sandbox Code Playgroud)

python filenames file wildcard

22
推荐指数
3
解决办法
6万
查看次数

标签 统计

file ×1

filenames ×1

python ×1

wildcard ×1