windows zip压缩报错includes characters that cannot be used in a compressed folder

本文介绍了解决Windows系统中使用zip压缩文件夹时遇到的因包含非法字符(如非ASCII字符)报错的问题,提供了一段Python脚本,通过os模块遍历文件夹并批量检测和重命名非ASCII字符的文件名。
摘要由CSDN通过智能技术生成

Windows 用 zip 压缩文件夹时报错:

'<文件>' cannot be compressed because includes characters that cannot be used in a compressed folder, such as <非法字符>. You should rename this file or directory.

同 [1]。考虑用 python 批量检测非 ascii 字符,并重命名。参考 [2] 检测非 ascii 字符。

  • 只改了文件名,没改目录名,可按需自加
import os, os.path as osp

def clean(s):
	"""清除文件名不合法字符"""
    s = ' '.join(s.split())
    s = s.replace('\n', '')
    s = s.replace('\t', '')
    s = s.replace('\r', '')
    s = ''.join([_s for _s in s if _s.isascii()])
    return s

P = r"C:\Users\itom\paper"
for root, dirs, files in os.walk(P):
    for f in files:
        if not f.isascii(): # 含非 ascii 字符
            new_f = clean(f)
            assert new_f.isascii(), f
            os.rename(osp.join(root, f), osp.join(root, new_f))

References

  1. Windows 10 - cannot compressed folders/files that contain hebrew characters in their name
  2. Detect strings with non English characters in Python
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值