简体   繁体   English

如何使用python查找大小超过指定限制的文件

[英]How to find a file which is having size more than the specified limit using python

How to find a file inside a folder, where the file size should be greater than the specified limit. 如何在文件夹中查找文件,该文件的大小应大于指定的限制。

for root,dirs,files in os.walk('/mnt/usbdrive/data'):
    for file in files:
        if os.stat( '/mnt/usbdrive/data/').st_size >= 1020.0:
            s3C.upload_file(os.path.join(root,file),BUCKET_NAME,file)
        else:
            print("no files found")
print ("data available")

Using os.path.getsize : 使用os.path.getsize

for root,dirs,files in os.walk('/mnt/usbdrive/data'):
    for file in files:
        if os.path.getsize(file) >= 1020.0:
            s3C.upload_file(os.path.join(root,file),BUCKET_NAME,file)
        else:
            print("no files found")
print ("data available")

EDIT: 编辑:

import os
thePath = os.getcwd()
theFiles = list(os.listdir(thePath))

theDict = dict()
for something in theFiles: #Calculate size for all files here.
    theStats = os.stat(something)
    theDict[something] = theStats

for item in theDict:
    if theDict[item].st_size > 1020:
        print("File: {}, size greater than 1020, Uploading to s3 ..".format(item))
        s3C.upload_file(os.path.join(root,file),BUCKET_NAME,file)
    else:
        print("File {}, size less than 1020".format(item))

OUTPUT (from my dir and with commented s3C.upload_file() method: 输出(从我的dir并带有s3C.upload_file()方法注释:

File: .idea, size greater than 1020, Uploading to s3 ..
File: celebs.jpg, size greater than 1020, Uploading to s3 ..
File data.csv, size less than 1020
File: dum.jpg, size greater than 1020, Uploading to s3 ..
File: dummy.jpg, size greater than 1020, Uploading to s3 ..
File example2.csv, size less than 1020

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM