• notice
  • Congratulations on the launch of the Sought Tech site

Solve Python save file name is too long OSError: [Errno36] Filenametoolon

Save file name is too long OSError: [Errno 36] File name too lon

Problem Description

Install

pip install python-docx

code

from docx import Document
Document().save('{}.docx'.format('1' * 256))

Error OSError: [Errno 36] File name too long

solution

Linux and Windows filenames are limited to approximately 255 characters

def shorten_filename(filename, limit=100):
    """Returns the file name of the appropriate length, with ... in the middle to display """
    if len(filename) <= limit:
        return filename
    else:
        return filename[:int(limit / 2) - 3] + '...' + filename[len(filename) - int(limit / 2):]
filename = '{}.txt'.format('Power is very smart
print(filename)
print(shorten_filename(filename))
# The pain itself is important, the main customer is the elite sit amet, consectetur adipiscing elitLorem dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum dolor sit amet,The main customer is the pain The customer is the pain the painThe main customer is the pain The customer is the pain the pain is funThe main customer is the pain The customer is the pain the pain is funThe main customer is the pain eliteThe main customer is the pain eliteThe main customer is the pain eliteThe main customer is the pain elite
# Lorem ipsum dolor sit amet, consectetur adipiscing elitLem ipsum dolor sit amet, consectetur adipiscing elitLorem ipsum do...

Python super long file name solution

Using python today, write a code to move files in batches!

As you can see, there are nearly 900 projects, several layers of directories, and there are more than 50 photos under one project, and these photos should be moved to the first layer of directories.

Originally my code was written like this:

import them
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)

The directory has several levels. The previous for loop will not be described in detail. I feel that the code is correct.

After running, it always reports an error, FileNotFoundError: [WinError 3] The system cannot find the specified path.

Later, it was found that the file name exceeded 255 characters.

The final solution is to change the relative path to an absolute path and add "\\?\" in front of the path.

Looking for the article of the predecessors, the summary is as follows:

  • The solution is to find a way to tell the computer that my path length is long. First, I checked the maximum length of the file path under Windows, and the result was 260. This is enough for most cases, but the problem of long file paths still exists.

  • Then I saw the definition of UNC. It turns out that the length of the file path under Windows can be up to more than 30,000. I can't remember exactly how much hahaha.

  • Just adding "\\?\" in front of the absolute path tells the computer that I need to use the maximum path length, so that there is no problem of inoperability caused by the path being too long.

How to add "\\?\" here is not too clear

After trying several times, I found that I need to add multiple \ to finally get the result. The code is as follows for reference:

import them
from shutil import move
path_list = os.listdir('./qi')
#Several loops to get the file name hidden in the bottom directory
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:
               # get absolute path
               old_path = os.path.abspath('./qi'+'/'+path+'/'+path1+'/'+img+'/')
               new_path = os.path.abspath('./qi'+'/'+path+'/')
               #Add material before the absolute path, you can identify
               oldfile = os.path.join('\\\\?\\'+old_path,png)
               newfile = os.path.join('\\\\?\\'+new_path,png)
               move(oldfile, newfile)


Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

purchase lipitor pill & lt;a href="https://lipiws.top/"& gt;atorvastatin order online& lt;/a& gt; buy lipitor 10mg pill

Bjrwko

2024-03-07

Leave a Reply

+