Python:目录搜索

2024-09-29 06:28:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在有一个包含python文件的文件夹系统py. 在

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

我的问题

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

二。我如何知道文件夹中的所有内容占用了多少容量并将其打印在屏幕上?在

谢谢!在

编辑: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

Tags: orin文件夹cmdyouinputifsave
2条回答

在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是一个空目录。见下文:

^{pr2}$

现在,说清楚,这又快又脏。你可能不会硬编码你要访问的列表的索引。在

有一个名为glob的python库可以完成这类工作,因此您不必重新发明轮子。它可以列出一个目录中的文件,按扩展名类型进行匹配,等等,这真是太好了。在

相关问题 更多 >