
影刀RPA 文件属性操作只读隐藏与时间戳作者林焱什么情况用什么需要设置文件为只读防止误修改、隐藏某些配置文件、修改文件的时间戳让排序更准确、检查文件权限判断是否可写入。在影刀RPA里用os和stat模块可以操作文件的各种属性。适用场景保护重要文件不被修改、隐藏配置文件、批量修改文件属性、检查文件权限。怎么做店群矩阵自动化突破运营极限只读属性importosimportstat# 设置文件为只读defset_readonly(filepath):设置文件只读os.chmod(filepath,stat.S_IREAD)# 取消只读defset_writable(filepath):取消只读恢复可写os.chmod(filepath,stat.S_IWRITE|stat.S_IREAD)# 检查是否只读defis_readonly(filepath):检查文件是否只读returnnotos.access(filepath,os.W_OK)# 批量设置只读defbatch_set_readonly(folder,pattern*):批量设置文件夹内文件为只读importfnmatch count0forfinos.listdir(folder):iffnmatch.fnmatch(f,pattern):filepathos.path.join(folder,f)ifos.path.isfile(filepath):set_readonly(filepath)count1print(f已设置{count}个文件为只读)returncount# 使用batch_set_readonly(rC:\Reports\final,*.xlsx)隐藏属性# Windows设置文件隐藏属性importctypesdefset_hidden(filepath,hiddenTrue):设置Windows文件隐藏属性ifhidden:# 添加隐藏属性ctypes.windll.kernel32.SetFileAttributesW(filepath,0x2)# FILE_ATTRIBUTE_HIDDENelse:# 移除隐藏属性ctypes.windll.kernel32.SetFileAttributesW(filepath,0x80)# FILE_ATTRIBUTE_NORMAL# 使用set_hidden(rC:\Config\secret.json,hiddenTrue)修改时间戳importosfromdatetimeimportdatetimedefset_file_time(filepath,modify_timeNone,access_timeNone): 修改文件时间戳 modify_time/access_time: datetime对象 ifmodify_timeisNone:modify_timedatetime.now()ifaccess_timeisNone:access_timemodify_time# 转为时间戳mtimemodify_time.timestamp()atimeaccess_time.timestamp()os.utime(filepath,(atime,mtime))# 使用把文件修改时间设为指定日期set_file_time(rC:\Data\report.xlsx,modify_timedatetime(2026,7,1,12,0,0))# 批量修改按文件名中的日期设置修改时间deffix_timestamps_by_name(folder):根据文件名中的日期修正修改时间importreforfinos.listdir(folder):filepathos.path.join(folder,f)ifnotos.path.isfile(filepath):continue# 从文件名提取日期如 report_20260701.xlsxmatchre.search(r(\d{4})(\d{2})(\d{2}),f)ifmatch:year,month,dayint(match.group(1)),int(match.group(2)),int(match.group(3))try:dtdatetime(year,month,day)set_file_time(filepath,dt)print(f{f}→{dt.strftime(%Y-%m-%d)})exceptValueError:pass获取文件属性defget_file_info(filepath):获取文件完整信息stat_infoos.stat(filepath)info{文件名:os.path.basename(filepath),路径:filepath,大小(字节):stat_info.st_size,大小(KB):round(stat_info.st_size/1024,2),大小(MB):round(stat_info.st_size/1024/1024,2),创建时间:datetime.fromtimestamp(stat_info.st_ctime).strftime(%Y-%m-%d %H:%M:%S),修改时间:datetime.fromtimestamp(stat_info.st_mtime).strftime(%Y-%m-%d %H:%M:%S),访问时间:datetime.fromtimestamp(stat_info.st_atime).strftime(%Y-%m-%d %H:%M:%S),是否只读:notos.access(filepath,os.W_OK),扩展名:os.path.splitext(filepath)[1],}returninfo# 批量获取文件夹信息defget_folder_info(folder):获取文件夹内所有文件信息importpandasaspd all_info[]forfinos.listdir(folder):filepathos.path.join(folder,f)ifos.path.isfile(filepath)andnotf.startswith(~$):infoget_file_info(filepath)all_info.append(info)dfpd.DataFrame(all_info)returndf# 使用dfget_folder_info(rC:\Data)df.to_excel(rC:\Data\文件信息.xlsx,indexFalse)影刀RPA属性操作流程【设置变量】 report_folder rC:\Reports\定稿 【执行Python代码】 # 1. 批量设置只读 batch_set_readonly(yd_var[report_folder], *.xlsx) # 2. 隐藏临时文件 for f in os.listdir(yd_var[report_folder]): if f.startswith(temp_) or f.startswith(~$): set_hidden(os.path.join(yd_var[report_folder], f), True) 【输出信息】 已保护X个文件只读 已隐藏Y个临时文件有什么坑temu店群自动化报活动案例坑1只读文件无法删除# 问题设了只读后删除文件报PermissionErroros.remove(filepath)# PermissionError# 解决先取消只读再删除set_writable(filepath)os.remove(filepath)# 或者在删除前统一检查defsafe_delete(filepath):安全删除文件ifos.path.exists(filepath):ifis_readonly(filepath):set_writable(filepath)os.remove(filepath)坑2隐藏文件在listdir中仍然可见# 问题设置了隐藏属性但os.listdir还是能列出来# os.listdir不区分隐藏属性# 解决用额外标记过滤forfinos.listdir(folder):filepathos.path.join(folder,f)# 检查隐藏属性attrsctypes.windll.kernel32.GetFileAttributesW(filepath)ifattrs0x2:# FILE_ATTRIBUTE_HIDDENcontinue# 跳过隐藏文件print(f)坑3os.utime时间戳精度# 问题设置的时间戳精度不够秒以下被截断# Windows的文件时间戳精度通常到秒# 设置精确到秒dtdatetime(2026,7,1,12,30,45)os.utime(filepath,(dt.timestamp(),dt.timestamp()))# 读取时可能变成 12:30:45 或 12:30:44坑4文件被占用无法修改属性# 问题Excel文件正被打开设置只读失败# 解决等待文件释放importtimedefset_readonly_safe(filepath,max_retries5):安全设置只读foriinrange(max_retries):try:os.chmod(filepath,stat.S_IREAD)returnTrueexceptPermissionError:print(f文件被占用等待重试({i1}/{max_retries}))time.sleep(2)returnFalse坑5Linux和Windows属性操作不同# Windows特有的隐藏属性在Linux上不存在# 需要判断操作系统importplatformifplatform.system()Windows:set_hidden(filepath,True)else:# Linux: 文件名以.开头即为隐藏# 需要重命名dirname,filenameos.path.split(filepath)ifnotfilename.startswith(.):new_pathos.path.join(dirname,.filename)os.rename(filepath,new_path)