title: 自动规整微信接收文件-python
tags:
categories: python
date: Mar 9, 2023 at 15:37:19
author: yeyezi
subject:
相信大家都体验过以"小而美"著称的微信, 这款神奇而伟大的软件无情的占据了每一个电脑的15g空间以上, 而鄙人的电脑总空间...只有256...
于是, 本人注定和微信有一场旷日持久的战斗.
微信与我而言最主要的问题有以下几点:
综上所述, 写一个自动化脚本将各个文件夹内的接受文件转移到单独文件夹内是非常合理的.
代码如下(macOS):
#!~/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表达式+命令进行自动化部署.
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 删除。