Python: 目录搜索

96 阅读2分钟

我有一个文件夹,里面有一个Python文件 sys.py。在相同的文件夹中,我有一个叫做 fsys(文件系统的缩写)的文件夹,我想查看 fsys 文件夹中包含的内容。

我的问题:

  1. 如何查看文件夹中的内容并将其打印在屏幕上?
  2. 如何查看文件夹中所有内容的容量并将其打印在屏幕上?
  3. 如何将文件夹列表做成蓝色?
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
  1. 解决方案 问题1和问题2的解决方案是使用 Python 的 os 库。os 库提供了许多有用的函数来操作文件和目录,包括 listdir()stat()
import os

# 获取当前目录
cwd = os.getcwd()

# 获取当前目录下的所有文件和目录
files = os.listdir(cwd)

# 遍历文件和目录,打印文件名和容量
for file in files:
    file_path = os.path.join(cwd, file)
    if os.path.isfile(file_path):
        # 打印文件名和容量
        print("File:", file)
        print("Size:", os.stat(file_path).st_size)

# 获取 `fsys` 目录下的所有文件和目录
fsys_files = os.listdir(os.path.join(cwd, "fsys"))

# 遍历文件和目录,打印文件名和容量
for file in fsys_files:
    file_path = os.path.join(cwd, "fsys", file)
    if os.path.isfile(file_path):
        # 打印文件名和容量
        print("File:", file)
        print("Size:", os.stat(file_path).st_size)

问题3的解决方案是使用 Python 的 colored 库。colored 库提供了许多有用的函数来给终端上的文本着色,包括 fg()bg()

from colored import fg, bg

# 获取 `fsys` 目录下的所有文件和目录
fsys_files = os.listdir(os.path.join(cwd, "fsys"))

# 遍历文件和目录,打印蓝色文件名和容量
for file in fsys_files:
    file_path = os.path.join(cwd, "fsys", file)
    if os.path.isfile(file_path):
        # 打印蓝色文件名和容量
        print(fg("blue"), "File:", file)
        print("Size:", os.stat(file_path).st_size)