首页 >>  正文

python文件打开的合法模式

来源:baiyundou.net   日期:2024-09-21

作者:大橡皮啊

最终效果:

       删除重复文件,并保留创建日期更为久远的文件

      上篇文章写了,利用群晖的存储空间分析器结合python来查找重复文件.经人提醒,发现完全不需要用到存储分析器.因为那个文章编辑次数到限制了.无法编辑了.只能重新开贴.   有一点小小的设置.看下图

      file_type是一个文件类型的列表.在里面添加你想要删除的文件类型即可.每个文件类型之间用逗号隔开.

import os

import hashlib

# 只删除以下列表中的重复文件类型.如果想删除其他类型的文件,自己添加一下就行了

file_type = ['.jpg', '.jpeg', '.png', '.gif', '.psd', '.bmp', '.webp', '.mp4', '.mkv', '.avi', '.mov', 'mpeg', 'mpg',

             '.rar', '.zip']

check_files = []

def remove_reapt_files():

    for root, dirs, files in os.walk(r'/volume1/homes/xxxxxx/Drive/Moments/'):

        for name in files:

            # print(name)

            p_type = os.path.splitext(os.path.join(root, name))[1]

            if p_type in file_type:

                check_files.append(os.path.join(root, name))

        for name in dirs:

            p_type = os.path.splitext(os.path.join(root, name))[1]

            if p_type in file_type:

                check_files.append(os.path.join(root, name))

    files_dict = {}

    r_index = 0

    print('Fiels Num:%s' % len(check_files))

    for value in check_files:

        md5_hash = hashlib.md5()

        try:

            with open(value, "rb+") as f:

                for byte_block in iter(lambda: f.read(4096), b""):

                    md5_hash.update(byte_block)

                file_md5 = md5_hash.hexdigest()

                print('Check file MD5:%s' % value)

            if files_dict.get(file_md5) is None:

                files_dict[file_md5] = value

            else:

                d_path = files_dict[file_md5]

                d_path_stats = os.stat(d_path)

                file_stats = os.stat(value)

                d_time = d_path_stats.st_ctime

                f_time = file_stats.st_ctime

                if d_time > f_time:

                    os.remove(d_path)

                    files_dict[file_md5] = value

                    print('Delete File:', d_path)

                    r_index += 1

                else:

                    os.remove(value)

                    print('Delete File:', value)

                    r_index += 1

        except Exception as e:

            pass

            #print('File does not exist or has been deleted')

    print('File Count:%s, Repeat Files Num:%s. All deleted!' %( len(check_files),str(r_index)))

if __name__ == '__main__':

    remove_reapt_files()

文件下载之后,要添加到计划任务中.

先打开群晖上的File Station 新建一个文件夹.我这边取名叫 python_shell.然后把脚本文件复制到文件夹下.

然后打开计划任务,添加一个计划任务.执行频率.看个人喜好.运行命令就写python3 加上脚本的路径

这样就大功告成了.看看效果吧.

@eadir文件夹是群晖全局索引文件.所以删除的文件要比实际的重复要多.因为索引也删除了.

","force_purephv":"0","gnid":"95c9e7329dc8eeac5","img_data":[{"flag":2,"img":[{"desc":"","height":"174","title":"","url":"https://p0.ssl.img.360kuai.com/t01d8789567fc7d07d3.jpg","width":"600"},{"desc":"","height":"259","title":"","url":"https://p0.ssl.img.360kuai.com/t01bcbb7a603d379e77.jpg","width":"600"},{"desc":"","height":"532","title":"","url":"https://p0.ssl.img.360kuai.com/t01bbe930fc857d6289.jpg","width":"533"},{"desc":"","height":"189","title":"","url":"https://p0.ssl.img.360kuai.com/t01cf226c23320badd5.jpg","width":"600"},{"desc":"","height":"164","title":"","url":"https://p0.ssl.img.360kuai.com/t01f0316e95e5b219f3.jpg","width":"600"}]}],"original":0,"pat":"art_src_1,fts0,sts0","powerby":"cache","pub_time":1668674208000,"pure":"","rawurl":"http://zm.news.so.com/5b931f4f25d4540121746449657c5822","redirect":0,"rptid":"ff74231c4c8790e1","s":"t","src":"什么值得买","tag":[],"title":"利用python删除群晖中的重复文件(2)

璩窦养3810python的open打开的是哪里的文件啊 -
台秦杭13577995192 ______ 因为路径的问题,就像你说的他并不知道你打开的文件路径.你可以在文件名前面加上绝对路径.建议学习一下os模块.你可以先 import os os.getcwd()看看当前路径是什么,如果不是想要的路径就用os.chdir(r'路径名')转移到需要的路径下,就可以直接输入文件名称打开了.

璩窦养3810如何在Python中打开文件后释放内存 -
台秦杭13577995192 ______ f = open('name', 'r', 'utf-8') do something f.close()

璩窦养3810Python程序,要求打开一个文本文件,从固定行开始读取一直读取到文件末尾 -
台秦杭13577995192 ______ for linenum, line in emumerate(open("path/to/the/file")): if linenum + 1 >= specific_linenum: do_something() 行数从一开始数,以上具体内容自行替换

(编辑:自媒体)
关于我们 | 客户服务 | 服务条款 | 联系我们 | 免责声明 | 网站地图 @ 白云都 2024