如何编写脚本以便他们可以读取内部txt文件 [英] How do I write a script so they can read inside txt files
本文介绍了如何编写脚本以便他们可以读取内部txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我需要编写一个脚本,让程序查看每个文件夹,查看.txt文件,读取一个数字,并将其存储到内存中。此外,我需要它循环,以便脚本可以运行并查看多个目录,以便它可以在文件中找到数字。这样,有了数字,我就可以制作一个条形图。
我尝试了什么:
我尝试了目录= os.fsencode(my_directory_that_info_is_in)
for os.listdir中的文件()
#这就是我现在能做的所有
So, I need to write a script that lets the program look into each folder, into the .txt file, read a number, and store it into its memory. Also, I need it to loop so the script can run and look at multiple directories so it can find the number in the files. That way, with the numbers, I can make a bar graph.
What I have tried:
I tried directory = os.fsencode(my_directory_that_info_is_in)
for file in os.listdir()
#That's all I can do for now
推荐答案
嗯,你根本不清楚你做不到的事情。如果您有一个目录中的文件列表,那么应该很容易依次读取每个文件。您需要将目录名称传递给 os.listdir()
才能获取列表。请参见 16.1。 os - 其他操作系统接口 - Python 3.6.2rc1文档 [ ^ ]。
Well it is not at all clear what you cannot do. If you have the list of files in a directory then it should be easy to read each one in turn. You need to pass the directory name to os.listdir()
in order to get the list. See 16.1. os — Miscellaneous operating system interfaces — Python 3.6.2rc1 documentation[^] .
这篇关于如何编写脚本以便他们可以读取内部txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文