时间:2022-10-16 11:24:20 | 栏目:Python代码 | 点击:次
安装
pip install python-docx
代码
from docx import Document Document().save('{}.docx'.format('1' * 256))
报错 OSError: [Errno 36] File name too long
Linux 和 Windows 文件名限制约为 255 个字符
def shorten_filename(filename, limit=100): """返回合适长度文件名,中间用...显示""" if len(filename) <= limit: return filename else: return filename[:int(limit / 2) - 3] + '...' + filename[len(filename) - int(limit / 2):] filename = '{}.txt'.format('Lorem ipsum dolor sit amet, consectetur adipiscing elit' * 30) print(filename) print(shorten_filename(filename)) # Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit.txt # Lorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum do...ipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elit.txt
今天用python,写一个批量移动文件的代码!
大家看到了,将近900个项目,好几层的目录,一个项目一下面有50几张的照片,要把这些照片集中移动到第一层的目录下面。
最初我的代码是这样写的:
import os from shutil import move path_list = os.listdir('./qi') for path in path_list: path_list1 = os.listdir('./qi/'+path) for path1 in path_list1: path_list2 = os.listdir('./qi/'+path+'/'+path1) for img in path_list2: png_list = os.listdir('./qi/'+path+'/'+path1+'/'+img) for png in png_list: oldfile = os.path.join('./qi'+'/'+path+'/'+path1+'/'+img+'/',png) newfile = os.path.join('./qi'+'/'+path+'/',png) move(oldfile,newfile)
目录有好几个层级,前面的 for 循环就不细述,感觉代码都没错。
运行后总是报错,FileNotFoundError: [WinError 3] 系统找不到指定的路径。
后面发现是文件名超过255位造成的。
最后的解决方法是将相对路径改为绝对路径,并在路径前方加上“\\?\”。
找了前辈的文章,摘要如下:
怎么加“\\?\”这里没有说太清楚了
试了好几次,发现要加好多个\总算得到结果,代码如下供参考:
import os from shutil import move path_list = os.listdir('./qi') #几次循环,获得藏在最底层目录的文件名 for path in path_list: path_list1 = os.listdir('./qi/'+path) for path1 in path_list1: path_list2 = os.listdir('./qi/'+path+'/'+path1) for img in path_list2: png_list = os.listdir('./qi/'+path+'/'+path1+'/'+img) for png in png_list: #获得绝对路径 old_path = os.path.abspath('./qi'+'/'+path+'/'+path1+'/'+img+'/') new_path = os.path.abspath('./qi'+'/'+path+'/') #在绝对路径前加料,就可以识别了 oldfile = os.path.join('\\\?\\'+old_path,png) newfile = os.path.join('\\\?\\'+new_path,png) move(oldfile,newfile)