实验验证Windows中不能用在文件名中的ASCII码
郝伟 2022/08/13
ASCII码中有个哪字符不能用在Windows的文件名中?
解释:Windows有文件名有一些限制,不是所有都可以用的,所以就有此问题。
在256个ASCII码中,共有41个字符无法使用,分别是32个控制字符和9个可见字符;
"*/:<>?\|/|\, <:> 和 *?"34 " 42 * 47 / 58 : 60 < 62 > 63 ? 92 \ 124 |:)虽然通过了测试,但是结果有明显的异常,所以也列为不可使用的常规字符。所以,结论就是32个控制字符和9个常规字符无法使用。控制字符是 0-31,而常规字符是:"*/:<>?\|,为了便于记忆,可以按调整顺序后分组:/|\, <:> 和 *?"。
ASCII码只有1个字节共256个,所以只要做个循环,逐一生成文件名进行检查,如果有不能作为文件名的,就把它打印出来,具体检查方法如下:
filename = V_X_.xt;;v1 = hello 写入文件 filename中;v2。如果 v1 == v2,则表示此字符能够正常使用,记录在列表 chs_ok 中,否则记录在列表 chs_err 中。
def write_txt_file(filename, content): ''' 将字符串 content 写入到文件 filename 中 ''' try: with open(filename, 'w') as f: f.write(content) return True except: return False return True def read_txt_file(filename): ''' 从文件 filename 中读取所有内容并返回 ''' try: with open(filename, "r") as f: # 打开文件 data = f.read() # 读取文件 return data except: return '' chs_ok = [] chs_err = [] for i in range(0, 256): v1 = 'hello' v2 = '' filepath=f'C:/data/chs/{i:0>d}_{chr(i)}_.txt' write_txt_file(filepath, v1) v2 = read_txt_file(filepath) if v1 == v2: chs_ok.append(i) else: chs_err.append(i) if i == 58: print(filepath, v2) print('# 正常的字符列表') print(len(chs_ok), chs_ok) print('\n# 有问题的字符列表') print(len(chs_err), chs_err) for v in [ 34, 42, 47, 60, 62, 63, 92, 124]: print(v, chr(v)) # 0-31 不可显示的字符无法使用,在可显示字符中有9个不可使用 # 34 " # 42 * # 47 / # 58 : # 60 < # 62 > # 63 ? # 92 \ # 124 | # 三个一组好记忆: / : \ * ? " < | > # 以下代码可以执行并且返回 12345 print('----------------- Test on char 58 -----------------') filename1 = r'C:/data/chs/058_:.txt' write_txt_file(filename1, '12345') print(read_txt_file(filename1))
输出如下
# 正常的字符列表
216 [32, 33, 35, 36, 37, 38, 39, 40, 41, 43, 44, 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
# 有问题的字符列表
40 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 34, 42, 47, 60, 62, 63, 92, 124]
在DOS中使用dir查看输出的文件列表,结果如下:
PS C:\Data\chs> dir 目录: C:\Data\chs Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2022/8/13 17:28 5 032_ .txt -a---- 2022/8/13 17:28 5 033_!.txt -a---- 2022/8/13 17:28 5 035_#.txt -a---- 2022/8/13 17:28 5 036_$.txt -a---- 2022/8/13 17:28 5 037_%.txt -a---- 2022/8/13 17:28 5 038_&.txt -a---- 2022/8/13 17:28 5 039_'.txt -a---- 2022/8/13 17:28 5 040_(.txt -a---- 2022/8/13 17:28 5 041_).txt -a---- 2022/8/13 17:28 5 043_+.txt -a---- 2022/8/13 17:28 5 044_,.txt -a---- 2022/8/13 17:28 5 045_-.txt -a---- 2022/8/13 17:28 5 046_..txt -a---- 2022/8/13 17:28 5 048_0.txt -a---- 2022/8/13 17:28 5 049_1.txt -a---- 2022/8/13 17:28 5 050_2.txt -a---- 2022/8/13 17:28 5 051_3.txt -a---- 2022/8/13 17:28 5 052_4.txt -a---- 2022/8/13 17:28 5 053_5.txt -a---- 2022/8/13 17:28 5 054_6.txt -a---- 2022/8/13 17:28 5 055_7.txt -a---- 2022/8/13 17:28 5 056_8.txt -a---- 2022/8/13 17:28 5 057_9.txt -a---- 2022/8/13 17:28 0 058_ -a---- 2022/8/13 17:28 5 059_;.txt -a---- 2022/8/13 17:28 5 061_=.txt -a---- 2022/8/13 17:28 5 064_@.txt -a---- 2022/8/13 17:28 5 065_A.txt -a---- 2022/8/13 17:28 5 066_B.txt -a---- 2022/8/13 17:28 5 067_C.txt -a---- 2022/8/13 17:28 5 068_D.txt -a---- 2022/8/13 17:28 5 069_E.txt -a---- 2022/8/13 17:28 5 070_F.txt -a---- 2022/8/13 17:28 5 071_G.txt -a---- 2022/8/13 17:28 5 072_H.txt -a---- 2022/8/13 17:28 5 073_I.txt -a---- 2022/8/13 17:28 5 074_J.txt -a---- 2022/8/13 17:28 5 075_K.txt -a---- 2022/8/13 17:28 5 076_L.txt -a---- 2022/8/13 17:28 5 077_M.txt -a---- 2022/8/13 17:28 5 078_N.txt -a---- 2022/8/13 17:28 5 079_O.txt -a---- 2022/8/13 17:28 5 080_P.txt -a---- 2022/8/13 17:28 5 081_Q.txt -a---- 2022/8/13 17:28 5 082_R.txt -a---- 2022/8/13 17:28 5 083_S.txt -a---- 2022/8/13 17:28 5 084_T.txt -a---- 2022/8/13 17:28 5 085_U.txt -a---- 2022/8/13 17:28 5 086_V.txt -a---- 2022/8/13 17:28 5 087_W.txt -a---- 2022/8/13 17:28 5 088_X.txt -a---- 2022/8/13 17:28 5 089_Y.txt -a---- 2022/8/13 17:28 5 090_Z.txt -a---- 2022/8/13 17:28 5 091_[.txt -a---- 2022/8/13 17:28 5 093_].txt -a---- 2022/8/13 17:28 5 094_^.txt -a---- 2022/8/13 17:28 5 095__.txt -a---- 2022/8/13 17:28 5 096_`.txt -a---- 2022/8/13 17:28 5 097_a.txt -a---- 2022/8/13 17:28 5 098_b.txt -a---- 2022/8/13 17:28 5 099_c.txt -a---- 2022/8/13 17:28 5 100_d.txt -a---- 2022/8/13 17:28 5 101_e.txt -a---- 2022/8/13 17:28 5 102_f.txt -a---- 2022/8/13 17:28 5 103_g.txt -a---- 2022/8/13 17:28 5 104_h.txt -a---- 2022/8/13 17:28 5 105_i.txt -a---- 2022/8/13 17:28 5 106_j.txt -a---- 2022/8/13 17:28 5 107_k.txt -a---- 2022/8/13 17:28 5 108_l.txt -a---- 2022/8/13 17:28 5 109_m.txt -a---- 2022/8/13 17:28 5 110_n.txt -a---- 2022/8/13 17:28 5 111_o.txt -a---- 2022/8/13 17:28 5 112_p.txt -a---- 2022/8/13 17:28 5 113_q.txt -a---- 2022/8/13 17:28 5 114_r.txt -a---- 2022/8/13 17:28 5 115_s.txt -a---- 2022/8/13 17:28 5 116_t.txt -a---- 2022/8/13 17:28 5 117_u.txt -a---- 2022/8/13 17:28 5 118_v.txt -a---- 2022/8/13 17:28 5 119_w.txt -a---- 2022/8/13 17:28 5 120_x.txt -a---- 2022/8/13 17:28 5 121_y.txt -a---- 2022/8/13 17:28 5 122_z.txt -a---- 2022/8/13 17:28 5 123_{.txt -a---- 2022/8/13 17:28 5 125_}.txt -a---- 2022/8/13 17:28 5 126_~.txt -a---- 2022/8/13 17:28 5 127__txt -a---- 2022/8/13 17:28 5 128__txt -a---- 2022/8/13 17:28 5 129__txt -a---- 2022/8/13 17:28 5 130__txt -a---- 2022/8/13 17:28 5 131__txt -a---- 2022/8/13 17:28 5 132__txt -a---- 2022/8/13 17:28 5 133__txt -a---- 2022/8/13 17:28 5 134__txt -a---- 2022/8/13 17:28 5 135__txt -a---- 2022/8/13 17:28 5 136__txt -a---- 2022/8/13 17:28 5 137__txt -a---- 2022/8/13 17:28 5 138__txt -a---- 2022/8/13 17:28 5 139__txt -a---- 2022/8/13 17:28 5 140__txt -a---- 2022/8/13 17:28 5 141__txt -a---- 2022/8/13 17:28 5 142__txt -a---- 2022/8/13 17:28 5 143__txt -a---- 2022/8/13 17:28 5 144__txt -a---- 2022/8/13 17:28 5 145__txt -a---- 2022/8/13 17:28 5 146__txt -a---- 2022/8/13 17:28 5 147__txt -a---- 2022/8/13 17:28 5 148__txt -a---- 2022/8/13 17:28 5 149__txt -a---- 2022/8/13 17:28 5 150__txt -a---- 2022/8/13 17:28 5 151__txt -a---- 2022/8/13 17:28 5 152__txt -a---- 2022/8/13 17:28 5 153__txt -a---- 2022/8/13 17:28 5 154__txt -a---- 2022/8/13 17:28 5 155__txt -a---- 2022/8/13 17:28 5 156__txt -a---- 2022/8/13 17:28 5 157__txt -a---- 2022/8/13 17:28 5 158__txt -a---- 2022/8/13 17:28 5 159__txt -a---- 2022/8/13 17:28 5 160_ .txt -a---- 2022/8/13 17:28 5 161_¡.txt -a---- 2022/8/13 17:28 5 162_¢.txt -a---- 2022/8/13 17:28 5 163_£.txt -a---- 2022/8/13 17:28 5 164_¤.txt -a---- 2022/8/13 17:28 5 165_¥.txt -a---- 2022/8/13 17:28 5 166_¦.txt -a---- 2022/8/13 17:28 5 167_§.txt -a---- 2022/8/13 17:28 5 168_¨.txt -a---- 2022/8/13 17:28 5 169_©.txt -a---- 2022/8/13 17:28 5 170_ª.txt -a---- 2022/8/13 17:28 5 171_«.txt -a---- 2022/8/13 17:28 5 172_¬.txt -a---- 2022/8/13 17:28 5 173_.txt -a---- 2022/8/13 17:28 5 174_®.txt -a---- 2022/8/13 17:28 5 175_¯.txt -a---- 2022/8/13 17:28 5 176_°.txt -a---- 2022/8/13 17:28 5 177_±.txt -a---- 2022/8/13 17:28 5 178_².txt -a---- 2022/8/13 17:28 5 179_³.txt -a---- 2022/8/13 17:28 5 180_´.txt -a---- 2022/8/13 17:28 5 181_µ.txt -a---- 2022/8/13 17:28 5 182_¶.txt -a---- 2022/8/13 17:28 5 183_·.txt -a---- 2022/8/13 17:28 5 184_¸.txt -a---- 2022/8/13 17:28 5 185_¹.txt -a---- 2022/8/13 17:28 5 186_º.txt -a---- 2022/8/13 17:28 5 187_».txt -a---- 2022/8/13 17:28 5 188_¼.txt -a---- 2022/8/13 17:28 5 189_½.txt -a---- 2022/8/13 17:28 5 190_¾.txt -a---- 2022/8/13 17:28 5 191_¿.txt -a---- 2022/8/13 17:28 5 192_À.txt -a---- 2022/8/13 17:28 5 193_Á.txt -a---- 2022/8/13 17:28 5 194_Â.txt -a---- 2022/8/13 17:28 5 195_Ã.txt -a---- 2022/8/13 17:28 5 196_Ä.txt -a---- 2022/8/13 17:28 5 197_Å.txt -a---- 2022/8/13 17:28 5 198_Æ.txt -a---- 2022/8/13 17:28 5 199_Ç.txt -a---- 2022/8/13 17:28 5 200_È.txt -a---- 2022/8/13 17:28 5 201_É.txt -a---- 2022/8/13 17:28 5 202_Ê.txt -a---- 2022/8/13 17:28 5 203_Ë.txt -a---- 2022/8/13 17:28 5 204_Ì.txt -a---- 2022/8/13 17:28 5 205_Í.txt -a---- 2022/8/13 17:28 5 206_Î.txt -a---- 2022/8/13 17:28 5 207_Ï.txt -a---- 2022/8/13 17:28 5 208_Ð.txt -a---- 2022/8/13 17:28 5 209_Ñ.txt -a---- 2022/8/13 17:28 5 210_Ò.txt -a---- 2022/8/13 17:28 5 211_Ó.txt -a---- 2022/8/13 17:28 5 212_Ô.txt -a---- 2022/8/13 17:28 5 213_Õ.txt -a---- 2022/8/13 17:28 5 214_Ö.txt -a---- 2022/8/13 17:28 5 215_×.txt -a---- 2022/8/13 17:28 5 216_Ø.txt -a---- 2022/8/13 17:28 5 217_Ù.txt -a---- 2022/8/13 17:28 5 218_Ú.txt -a---- 2022/8/13 17:28 5 219_Û.txt -a---- 2022/8/13 17:28 5 220_Ü.txt -a---- 2022/8/13 17:28 5 221_Ý.txt -a---- 2022/8/13 17:28 5 222_Þ.txt -a---- 2022/8/13 17:28 5 223_ß.txt -a---- 2022/8/13 17:28 5 224_à.txt -a---- 2022/8/13 17:28 5 225_á.txt -a---- 2022/8/13 17:28 5 226_â.txt -a---- 2022/8/13 17:28 5 227_ã.txt -a---- 2022/8/13 17:28 5 228_ä.txt -a---- 2022/8/13 17:28 5 229_å.txt -a---- 2022/8/13 17:28 5 230_æ.txt -a---- 2022/8/13 17:28 5 231_ç.txt -a---- 2022/8/13 17:28 5 232_è.txt -a---- 2022/8/13 17:28 5 233_é.txt -a---- 2022/8/13 17:28 5 234_ê.txt -a---- 2022/8/13 17:28 5 235_ë.txt -a---- 2022/8/13 17:28 5 236_ì.txt -a---- 2022/8/13 17:28 5 237_í.txt -a---- 2022/8/13 17:28 5 238_î.txt -a---- 2022/8/13 17:28 5 239_ï.txt -a---- 2022/8/13 17:28 5 240_ð.txt -a---- 2022/8/13 17:28 5 241_ñ.txt -a---- 2022/8/13 17:28 5 242_ò.txt -a---- 2022/8/13 17:28 5 243_ó.txt -a---- 2022/8/13 17:28 5 244_ô.txt -a---- 2022/8/13 17:28 5 245_õ.txt -a---- 2022/8/13 17:28 5 246_ö.txt -a---- 2022/8/13 17:28 5 247_÷.txt -a---- 2022/8/13 17:28 5 248_ø.txt -a---- 2022/8/13 17:28 5 249_ù.txt -a---- 2022/8/13 17:28 5 250_ú.txt -a---- 2022/8/13 17:28 5 251_û.txt -a---- 2022/8/13 17:28 5 252_ü.txt -a---- 2022/8/13 17:28 5 253_ý.txt -a---- 2022/8/13 17:28 5 254_þ.txt -a---- 2022/8/13 17:28 5 255_ÿ.txt PS C:\Data\chs>
冒号比较奇怪,居然可以写入并读取,且值是正确的。但是文件名显示不正常,且大小为0,具体原理未能调研清楚,暂时的结论就是会造成奇怪的异常,所以不要使用。
C:\Users\hao>systeminfo
主机名: DESKTOP-ETAN7CP
OS 名称: Microsoft Windows 11 家庭版
OS 版本: 10.0.22000 暂缺 Build 22000
OS 制造商: Microsoft Corporation
OS 配置: 独立工作站
OS 构建类型: Multiprocessor Free
注册的所有人: hao
注册的组织: 暂缺
产品 ID: 00325-96524-01703-AAOEM
初始安装日期: 2022/6/19, 17:16:58
系统启动时间: 2022/7/13, 10:31:33
系统制造商: COLORFUL
系统型号: X15 AT 22
系统类型: x64-based PC
处理器: 安装了 1 个处理器。
[01]: Intel64 Family 6 Model 154 Stepping 3 GenuineIntel ~2300 Mhz
BIOS 版本: INSYDE Corp. 1.07.02TOYC9, 2022/2/22
Windows 目录: C:\WINDOWS
系统目录: C:\WINDOWS\system32
启动设备: \Device\HarddiskVolume1
系统区域设置: zh-cn;中文(中国)
输入法区域设置: en-us;英语(美国)
时区: (UTC+08:00) 北京,重庆,香港特别行政区,乌鲁木齐
物理内存总量: 32,472 MB
可用的物理内存: 16,508 MB
虚拟内存: 最大值: 49,880 MB
虚拟内存: 可用: 18,864 MB
虚拟内存: 使用中: 31,016 MB
页面文件位置: C:\pagefile.sys
域: WORKGROUP
登录服务器: \\DESKTOP-ETAN7CP
修补程序: 安装了 6 个修补程序。
[01]: KB5015732
[02]: KB5008295
[03]: KB5012170
[04]: KB5015814
[05]: KB5016353
[06]: KB5015898
网卡: 安装了 4 个 NIC。
[01]: Intel(R) Ethernet Connection (16) I219-LM
连接名: 以太网
状态: 媒体连接已中断
[02]: Intel(R) Wi-Fi 6E AX211 160MHz
连接名: WLAN
启用 DHCP: 是
DHCP 服务器: 192.168.2.200
IP 地址
[01]: 192.168.2.45
[02]: fe80::a4a3:b4cb:957c:1ad5
[03]: Bluetooth Device (Personal Area Network)
连接名: 蓝牙网络连接
状态: 媒体连接已中断
[04]: Sangfor SSL VPN CS Support System VNIC
连接名: 以太网 3
状态: 媒体连接已中断
Hyper-V 要求: 已检测到虚拟机监控程序。将不显示 Hyper-V 所需的功能。
C:\Users\hao>
PS, 使用 tasklist 查看当前进程信息。