Python:目录搜索

时间:2013-12-30 19:12:36

标签: python file directory

我目前有一个带有python文件sys.py的文件夹。

在同一个文件夹中,我有一个名为“fsys”的文件夹,用于文件系统,我需要检查fsys文件夹中的项目。

我的问题

予。如何查看文件夹中的内容并将其打印在屏幕上?

II。如何查看文件夹中的所有内容占用多少并将其打印在屏幕上?

谢谢!

编辑:III。如何将目录列表设为蓝色?

我的代码如下:

usrn = "user"
cli = "konix>"
st = 1
vs = "Konix One 0.01"
# - System Declarations -
import os
from os import listdir
from os.path import isfile, join
# - Title Declaration -
print "- K O N I X -"
print ""
print "- Konix One 0.01 -"
print ""
# - Command Reading - #
while st == 1:
    ucmd = raw_input(cli)
    cmd = ucmd.partition(' ')[0]
    if cmd == "print" or cmd == "pt":
        print ucmd.partition(' ')[2]
    if cmd == "version" or cmd == "vs":
        print vs
    if cmd == "tedit" or cmd == "td":
        if ucmd.partition(' ')[2] == "-h" or ucmd.partition(' ')[2] == "-help":
            print "Konix Tedit v1.2 Help"
            print "To open tedit, type in tedit -u in the console"
            print "You should see that your text pointer also has @tedit"
            print "To use tedit, first type in text normally."
            print "When you are done writing text, create two spaces, put a period, and press enter again."
            print "It will show you what you wrote, and you have the option to save or not."
            print "Good luck using tedit!"
        elif ucmd.partition(' ')[2] == "-u":
            input_list = []
            while True:
                input_str = raw_input("konix@tedit>")
                if input_str == "." and input_list[-1] == "":
                    break
                else:
                    input_list.append(input_str)
            for line in input_list:
                print line
            save = raw_input("Would you like to save this text to your file? [Y/N]: ")
            if save == "Y" or save == "y":
                name = raw_input("Please enter a name for this file (with .txt at the end): ")
                fsys = "fsys/"
                fsys += name
                filestring = '\n'.join(input_list)
                with open(fsys, 'w') as f:
                    f.write(filestring)
            elif save != "N" or save != "n":
                print "Not saving"
    if cmd == "list" or cmd == "ls":
        onlyfiles = [ f for f in listdir("fsys") if isfile(join("fsys",f)) ]
                print os.path.join(root, file)
    if cmd == "kill" or cmd == "kl":
        print "Killing Konix - Farewell!"
        st = 0

2 个答案:

答案 0 :(得分:2)

有一个名为glob的python库会执行这类工作,因此您无需重新发明轮子。它可以列出目录中的文件,按扩展名类型匹配等。很多好东西。

答案 1 :(得分:1)

在iPython中快速而肮脏,让你前进。

In [1]: import os

In [2]: cwd = os.getcwd()

In [3]: files = os.listdir(cwd)

In [4]: files
Out[4]: ['Hello.taskpaper', 'My Todos.taskpaper', 'Tips & Tricks.taskpaper']

In [5]: os.stat(cwd)
Out[5]: posix.stat_result(st_mode=16877, st_ino=393413, st_dev=16777217L, st_nlink=5, st_uid=501, st_gid=20, st_size=170, st_atime=1388431093, st_mtime=1362280774, st_ctime=1362280774)

In [6]: for f in files:
   ...: 
Display all 324 possibilities? (y or n) 
   ...: 
   ...:     print os.stat(f)
   ...: 
posix.stat_result(st_mode=33188, st_ino=406919, st_dev=16777217L, st_nlink=1, st_uid=501, st_gid=20, st_size=437, st_atime=1387857115, st_mtime=1341158105, st_ctime=1345503808)
posix.stat_result(st_mode=33188, st_ino=405425, st_dev=16777217L, st_nlink=1, st_uid=501, st_gid=20, st_size=238, st_atime=1387857115, st_mtime=1350748953, st_ctime=1350783773)
posix.stat_result(st_mode=33188, st_ino=414444, st_dev=16777217L, st_nlink=1, st_uid=501, st_gid=20, st_size=2560, st_atime=1387857115, st_mtime=1341158105, st_ctime=1345503292)

要查看子目录中的文件,请从列表中传入子目录。在我的例子中,fsys是一个空目录。见下文:

In [1]: import os

In [2]: cwd = os.getcwd()

In [3]: files = os.listdir(cwd)

In [4]: files
Out[4]: ['fsys', 'Hello.taskpaper', 'My Todos.taskpaper', 'Tips & Tricks.taskpaper']

In [5]: files_fsys = os.listdir(files[1])
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-5-7ca409829c27> in <module>()
----> 1 files_fsys = os.listdir(files[1])

OSError: [Errno 20] Not a directory: 'Hello.taskpaper'

In [6]: files_fsys = os.listdir(files[0])

In [7]: files_fsys
Out[7]: []

现在,要清楚,这是快速而肮脏的。您可能不会硬编码您尝试访问的列表的索引。