【问题标题】:Python script file list with specific file size具有特定文件大小的 Python 脚本文件列表
【发布时间】:2022-10-04 20:54:16
【问题描述】:

我正在尝试使用下面的 python 脚本加密我的目标目录中的所有文件,包括其子目录文件。我还提到了要在脚本中列出的特定文件大小,例如,列出所有大小不超过 8gb 的文件。

limit = raw_input('8GB: ')
    if int(limit) > 0:
import os
os.stat('E:/test4').st_size
from cryptography.fernet import Fernet

your_files = []

# os.walk gives us a 3-tuple 
for root, dirs, files in os.walk("E:/test4"):
    # go through all the files we found
    for file in files:
        size=os.path.getsize ( os.path.join( path, file )
        if size > limit :
        print(f"Simple filename: {file}"))
        # the if the file is called "log" or has a ".py" in it skip it
        if file == "log" or ".py" in file:
            continue
        # if not we can add it to our list
        # your_files.append(file)

        # if you need the full path of the file you can do this
        full_file_path = os.path.join(root, file)
        your_files.append(full_file_path)
        print(f"Full path: {file}")

print(f"Your files in a list{your_files}")
# have a look at the files list too, os.walk() creates it for you
print(your_files)


key = Fernet.generate_key()

with open("thekey.key", "wb") as thekey:
    thekey.write(key)

file_exts = [".log",".chm"]
for file in your_files:
       for ext in file_exts:
            if file.endswith(ext):
                  with open(file, "rb") as thefile:
                           contents = thefile.read()
                           contents_encrypted = Fernet(key).encrypt(contents)
                  with open(file, "wb") as thefile:
                          thefile.write(contents_encrypted)
   
print("Congratulation all files have been cleared successfully") 

但是得到下面的错误。需要一些建议我做错了什么。

File "E:\clear.py", line 2
    if int(limit) > 0:
IndentationError: unexpected indent

【问题讨论】:

  • if 语句不应缩进。下面的代码块应该。
  • @RonaldvanElburg 应该在哪里 if

标签: python script


【解决方案1】:
# Imports go on top
import os
from cryptography.fernet import Fernet

limit = raw_input('8GB: ')

# No indent before if    
if int(limit) > 0:
    # code block should be indented
    os.stat('E:/test4').st_size
    your_files = []
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 2015-10-13
    相关资源
    最近更新 更多