newb问题:文件搜索 [英] newb question: file searching
本文介绍了newb问题:文件搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Python的新手,我需要一些建议。我想要编写的脚本的一部分需要知道脚本路径中某个
扩展名的所有文件,并且所有子文件都是目录。可以有人
让我走上正确的道路,通过哪些模块和调用来做到这一点?
你会认为这是一个相当简单的命题,但是我不能在任何地方查找示例。谢谢。
I''m new at Python and I need a little advice. Part of the script I''m
trying to write needs to be aware of all the files of a certain
extension in the script''s path and all sub-directories. Can someone
set me on the right path to what modules and calls to use to do that?
You''d think that it would be a fairly simple proposition, but I can''t
find examples anywhere. Thanks.
推荐答案
嘿,我做过类似的事情。
你可以使用以下系统命令
导入os
os.system(''命令在这里'')
你可能想要做一些像
dir_name =''mydirectory''
import os
os .system(''ls''+ dir_name +''lsoutput.tmp'')
fin = open(''lsoutput.tmp'',''r'')
file_list = fin.readlines()
fin.close()
现在你有一个存储在file_list中的dir_name中所有文件的列表。
然后你必须用字符串方法解析输入。他们很容易在python中获得
。以下是它们的列表:
http:/ /docs.python.org/lib/string-methods.html
从os.system获取数据可能有更好的方法
命令,但我还没弄明白。相反,我正在做的是
将stdio输出写入文件并读入数据。它有效。
罚款。如果你在Linux中,请把它放在你的tmp目录中。
ja ******* @ gmail。 com 写道:
Hey, I''ve done similar things.
You can use system comands with the following
import os
os.system(''command here'')
You''d maybe want to do something like
dir_name = ''mydirectory''
import os
os.system(''ls '' +dir_name + '' lsoutput.tmp'')
fin = open(''lsoutput.tmp'', ''r'')
file_list = fin.readlines()
fin.close()
Now you have a list of all the files in dir_name stored in file_list.
Then you''ll have to parse the input with string methods. They''re easy
in python. Here''s the list of them:
http://docs.python.org/lib/string-methods.html
There is probably a better way to get the data from an os.system
command but i haven''t figured it out. Instead what i''m doing is
writing the stdio output to a file and reading in the data. It works
fine. Put it in your tmp dir if you''re in linux.
ja*******@gmail.com wrote:
我是Python的新手,我需要一些建议。我想要编写的脚本的一部分需要知道脚本路径中某个
扩展名的所有文件,并且所有子文件都是目录。可以有人
让我走上正确的道路,通过哪些模块和调用来做到这一点?
你会认为这是一个相当简单的命题,但是我不能在任何地方查找示例。谢谢。
I''m new at Python and I need a little advice. Part of the script I''m
trying to write needs to be aware of all the files of a certain
extension in the script''s path and all sub-directories. Can someone
set me on the right path to what modules and calls to use to do that?
You''d think that it would be a fairly simple proposition, but I can''t
find examples anywhere. Thanks.
嘿,我做过类似的事情。
您可以使用以下系统命令
导入os
os.system(''命令在这里'')
你可能想要做一些像
dir_name =''mydirectory''
import os
os.system(''ls'' + dir_name +''lsoutput.tmp'')
fin = open(''lsoutput.tmp'',''r'')
file_list = fin.readlines ()
fin.close()
现在你有一个存储在file_list中的dir_name中所有文件的列表。
然后你必须用字符串方法解析输入。他们很容易在python中获得
。以下是它们的列表:
http:/ /docs.python.org/lib/string-methods.html
从os.system获取数据可能有更好的方法
命令,但我还没弄明白。相反,我正在做的是
将stdio输出写入文件并读入数据。它有效。
罚款。如果你在Linux中,请把它放在你的tmp目录中。
ja ******* @ gmail。 com 写道:
Hey, I''ve done similar things.
You can use system comands with the following
import os
os.system(''command here'')
You''d maybe want to do something like
dir_name = ''mydirectory''
import os
os.system(''ls '' +dir_name + '' lsoutput.tmp'')
fin = open(''lsoutput.tmp'', ''r'')
file_list = fin.readlines()
fin.close()
Now you have a list of all the files in dir_name stored in file_list.
Then you''ll have to parse the input with string methods. They''re easy
in python. Here''s the list of them:
http://docs.python.org/lib/string-methods.html
There is probably a better way to get the data from an os.system
command but i haven''t figured it out. Instead what i''m doing is
writing the stdio output to a file and reading in the data. It works
fine. Put it in your tmp dir if you''re in linux.
ja*******@gmail.com wrote:
我是Python的新手,我需要一些建议。我想要编写的脚本的一部分需要知道脚本路径中某个
扩展名的所有文件,并且所有子文件都是目录。可以有人
让我走上正确的道路,通过哪些模块和调用来做到这一点?
你会认为这是一个相当简单的命题,但是我不能在任何地方查找示例。谢谢。
I''m new at Python and I need a little advice. Part of the script I''m
trying to write needs to be aware of all the files of a certain
extension in the script''s path and all sub-directories. Can someone
set me on the right path to what modules and calls to use to do that?
You''d think that it would be a fairly simple proposition, but I can''t
find examples anywhere. Thanks.
ja ***** **@gmail.com 写道:
我是Python的新手,我需要一些建议。我想要编写的脚本的一部分需要知道脚本路径中某个
扩展名的所有文件,并且所有子文件都是目录。
I''m new at Python and I need a little advice. Part of the script I''m
trying to write needs to be aware of all the files of a certain
extension in the script''s path and all sub-directories.
你想要的是os.walk()。
http://www.python.org/doc/current/lib/os-file-dir.html
What you want is os.walk().
http://www.python.org/doc/current/lib/os-file-dir.html
这篇关于newb问题:文件搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文