我正在用Python3编写一个小脚本,将超过31天的文件从我的下载文件夹移动到另一个文件夹(以便删除它们)。由于我有一些俄语和阿拉伯语的文件和文件夹,我在控制台中遇到以下错误:
File "C:\Users\mycomputer\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 13-16: character maps to <undefined>
以下是我的python代码:
# -*- coding: utf-8 -*-
import os, time, sys
import shutil
from datetime import datetime, date, timedelta
current_time = time.time()
days_old = 31 # Files that are 31 days old
days = (days_old*24*60*60) # We need to convert the number of days in seconds in order to use it within stat_time.atime
time_delete = current_time - days
source_path = "C:/Users/mycomputer/Desktop/python_move_temp/"
dest_path = "C:/Users/mycomputer/Desktop/python_moved_from_temp/"
logfile = "C:/Users/mycomputer/Desktop/python_moved_from_temp/" # Logging all the actions in the log file to see what happened
files_to_delete = []
for file in os.listdir(source_path):
#print(os.stat(source_path+i).st_mtime, i, "was last modified on")
#print(i, "- was last modified on ", os.stat(source_path+i).st_mtime)
if os.stat(source_path+file).st_mtime < time_delete:
# >> shutil.move(source_path+file, dest_path+file)
files_to_delete.append(file)
print("DELETING",file, time_delete) # Files that are going to be kept
print("Successfully deleted", len(files_to_delete), "file(s)")
我尝试了很多选择,但我不知道如何让它工作
注意:我仍然是Python的乞丐,因为我还在学习,代码可能很糟糕或者写得不好:)
目前没有回答
相关问题 更多 >
编程相关推荐