Python 将文件名列表的输出打印到文本文件

Python 将文件名列表的输出打印到文本文件,python,Python,我制作了一个Python程序,用于检查文件夹中的文件,并返回以.txt结尾的文件列表。现在我想把它输出到一个文件中。这是我的密码: import os import os.path import sys f=open("E:\\test.txt",'w') f.write('') path=os.path.abspath("E:\\test") def print_result(): print(name) for root, dirs, files in os.walk(path)

我制作了一个Python程序,用于检查文件夹中的文件,并返回以.txt结尾的文件列表。现在我想把它输出到一个文件中。这是我的密码:

import os
import os.path
import sys

f=open("E:\\test.txt",'w')
f.write('')

path=os.path.abspath("E:\\test")
def print_result():
    print(name)
for root, dirs, files in os.walk(path):
    for name in files:
        if name.endswith(".txt"):
            print_result()

Version 1:

alist=[print_name]            
alist.append(print_result)
f=open("E:\\test.txt", 'a')
f.writelines(alist)

Version 2:

alist=name
alist.append(name)
f = open("E:\\test.txt",'a')
print (name)

Version 3:

frame=print_result
saveout = sys.stdout
fsock = open("E:\\test.txt", 'a')
sys.stdout = fsock
print (frame)
sys.stdout = saveout`enter code here`

As you can see i have tried diferent types. The thing is that i only get the following in the file, instead of the actual output list:

<function print_result at 0x004F6DB0>
导入操作系统
导入操作系统路径
导入系统
f=打开(“E:\\test.txt”,“w”)
f、 写(“”)
path=os.path.abspath(“E:\\test”)
def print_result():
印刷品(名称)
对于os.walk(路径)中的根、目录和文件:
对于文件中的名称:
如果name.endswith(“.txt”):
打印结果()
第1版:
alist=[打印名称]
alist.append(打印结果)
f=打开(“E:\\test.txt”,“a”)
f、 书面语(alist)
第2版:
alist=名称
alist.append(名称)
f=打开(“E:\\test.txt”,“a”)
印刷品(名称)
第3版:
帧=打印结果
saveout=sys.stdout
fsock=open(“E:\\test.txt”,“a”)
sys.stdout=fsock
打印(帧)
sys.stdout=saveout`在此处输入代码`
正如你所看到的,我试过不同的类型。问题是,我在文件中只得到以下内容,而不是实际的输出列表:

这可以使用以下方法简单一点:


您需要获取目录中的所有文件

假设您的目录路径为“/home/ubuntu/test”,并且您希望将结果写入文件“result.txt”

然后进入“/home/ubuntu/”并打开result.txt。您将在那里输入文件名


要以递归方式访问当前工作目录中的*.txt文件,也要访问任何子目录中的*.txt文件(我认为您正试图通过
os.walk()
引用实现这一点),请尝试以下操作:

import os
def GetFiles(dir,f):
    basedir = dir
    subdirs = []
    for fname in os.listdir(dir):
        fileName = os.path.join(basedir, fname)
        if os.path.isfile(fileName):
            if fileName.endswith(".txt"):
                f.write(fileName+"\n")
                print fileName
        elif os.path.isdir(fileName):
            subdirs.append(fileName)
    for subdir in subdirs:
        GetFiles(subdir,f)
f=open("test.txt",'w')
GetFiles('./',f)
f.close()

这将列出当前工作目录和所有子目录中以“.txt”结尾的文件,并将路径和文件名放在文件test.txt中

为什么要定义一个只进行打印的函数
print\u result
?只需
print(name)
而不是
print\u result()
。你看过关于文件I/O的任何教程或文档吗?他们都会帮助你完成这项基本任务。我很感激OP已经接受了这一点,但原始代码使用了
os.walk()
,因此使用
glob
不会产生相同的结果,因为它不会深入到子目录中。@mihaighpu如果你认为这是正确的答案,你应该通过点击旁边的勾号来接受它
from os import listdir
from os.path import isfile, join

onlyfiles = [ f for f in listdir("/home/ubuntu/test") if isfile(join("/home/ubuntu/test",f)) ]

with open ("/home/ubuntu/result.txt","w")as fp:
  for line in onlyfiles:
    fp.write(line+"\n")
import os
def GetFiles(dir,f):
    basedir = dir
    subdirs = []
    for fname in os.listdir(dir):
        fileName = os.path.join(basedir, fname)
        if os.path.isfile(fileName):
            if fileName.endswith(".txt"):
                f.write(fileName+"\n")
                print fileName
        elif os.path.isdir(fileName):
            subdirs.append(fileName)
    for subdir in subdirs:
        GetFiles(subdir,f)
f=open("test.txt",'w')
GetFiles('./',f)
f.close()