前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自动规整微信接收文件-python

自动规整微信接收文件-python

原创
作者头像
叶子Tenney
发布2023-03-09 17:54:13
1K0
发布2023-03-09 17:54:13
举报

title: 自动规整微信接收文件-python

tags:

  • Python
  • 小工具
  • Mac OS

categories: python

date: Mar 9, 2023 at 15:37:19

author: yeyezi

subject:


相信大家都体验过以"小而美"著称的微信, 这款神奇而伟大的软件无情的占据了每一个电脑的15g空间以上, 而鄙人的电脑总空间...只有256...

微信的接受文件夹
微信的接受文件夹

于是, 本人注定和微信有一场旷日持久的战斗.

微信与我而言最主要的问题有以下几点:

  1. 在微信中下载的文件如果在微信中直接打开会变成只读无法直接编辑
  2. 微信下载文件分散在各个文件夹内, 甚至不同人发送的不同文件都会占用同一份内存
  3. 当同名文件发送, 微信会默默的在文件名后面加上一个"(1)"

综上所述, 写一个自动化脚本将各个文件夹内的接受文件转移到单独文件夹内是非常合理的.

代码如下(macOS):

代码语言:txt
复制
#!~/opt/anaconda3/bin/python

# -\*- coding: utf-8 -\*-



# Import the necessary modules

import os

import shutil



# Define the file path to the source directory

filePath="/Users/sandy/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/e5935df9a7a640bff14105be4661fcb4/Message/MessageTemp"

dst\_dir = "/Users/sandy/Library/Mobile Documents/com~apple~CloudDocs/Downloads/wechatDownloadFiles"



# Get a list of files in the source directory

get\_files\_sourceDir = os.listdir(filePath)



# Iterate over each file in the source directory

for file\_sourceDir in get\_files\_sourceDir:

    # Define the path to the file within the source directory

    file\_source = filePath+"/"+file\_sourceDir+"/File"



    # Check if the file exists

    if os.path.exists(file\_source):

        # Get a list of files in the file\_source directory

        get\_files = os.listdir(file\_source)



        # Iterate over each file in the file\_source directory

        for file\_ in get\_files:

            # Define the source and destination file paths

            src\_file = os.path.join(file\_source, file\_)

            dst\_file = os.path.join(dst\_dir, file\_)



            # Check if the destination file exists

            if os.path.exists(dst\_file):

                # Check if the source and destination files are the same

                if os.path.samefile(src\_file, dst\_file):

                    # If they are the same, skip this iteration of the loop

                    continue

                # If they are not the same, remove the destination file

                os.remove(dst\_file)



            # Move the file from the source directory to the destination directory

            shutil.move(file\_source + "/" + file\_, dst\_dir)



            # Print the name of the file that was moved

            print(file\_)

    else:

        # If the file does not exist, print an error message

        print("The file does not exist")

 

微信的默认接受文件夹可以在微信中接受文件后右键获取.

如果是macOS可以使用自带的crontab进行自动化运行, 使用方法是在terminal中输入crontab -e, 而后使用cron表达式+命令进行自动化部署.

代码语言:txt
复制
0 23 \* \* \* /Users/sandy/opt/anaconda3/bin/python "/Users/sandy/Nutstore Files/Nutstore/shell/moveWechatFiles.py"

0 23 \* \* \*是cron表达式, 代表每天23点.

python "moveWechatFiles.py"代表运行命令, 注意可以使用绝对路径来避免运行失败.

另外, 可以在之后加上>/dev/null 2>&1以去除运行日志减少空间利用.

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档