将文件夹和文件夹的文件夹写入使用Python 3.7的文本文件
我正在使用Python 3.7。 我想检查文本文件的文件路径的目录的文件大小。
这是我的代码:
import os
import glob
# folder path
folderpath = 'C:/Test'
# Get a list of files in my folder
list_of_files = filter( os.path.isfile,
glob.glob(folderpath + '*') )
# get list of files with the size in my folder
files_with_size = [ (file_path, os.stat(file_path).st_size)
for file_path in list_of_files ]
# Iterate over the files and write them to a file
for file_path, file_size in files_with_size:
with open('c:/Test/filesize.txt', 'w') as f:
print(file_size, ' -->', file_path)
我可以在Python控制台中打印结果,但是我无法将结果写入文本文件。 有人可以帮我吗?
问候, 扬
I am using Python 3.7.
I want to check the file sizes of a directory with the file path to a text file.
This is my code:
import os
import glob
# folder path
folderpath = 'C:/Test'
# Get a list of files in my folder
list_of_files = filter( os.path.isfile,
glob.glob(folderpath + '*') )
# get list of files with the size in my folder
files_with_size = [ (file_path, os.stat(file_path).st_size)
for file_path in list_of_files ]
# Iterate over the files and write them to a file
for file_path, file_size in files_with_size:
with open('c:/Test/filesize.txt', 'w') as f:
print(file_size, ' -->', file_path)
I can print the result in the Python console, but I cannot manage to write my result to a text file.
Can anybody help me?
Regards,
Jan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会将您的代码遵循以下方式
变更:使用
a
(附加)模式,然后使用f
作为new hehref = of note grigent file 的值“ https://docs.python.org/3/library/functions.html#print” rel =“ nofollow noreferrer”>print> print
函数。I would ameloriate your code following way
changes: use
a
(append) mode and usef
as value for named argumentfile
ofprint
function.