Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Traversing a filesystem. Strange Issue

I am trying to traverse a directory. Below is the code:

file_list = []
    os.chdir(self.config.Root_Directory_Path())
    for root, dirs, files in os.walk("."):
        file_list.extend( join(root,f) for f in files )
    file_sorted = sorted(file_list)
    f = open(self.config.Client_Local_Status(),'wb')        
    for file in file_sorted:
        print(file + "|" + str(os.path.getmtime(file)) + "\n")            
    f.close()

Firstly, I traverse the tree, then sort it and then print it. But I get the below error while traversing. I am very sure that the file exist, but not able to figure out the reason for error. PLEASE HELP ME FIGURE OUT REASON FOR ERROR AND HENCE FIX IT.

Below is the output.

Output:

.\Drivers\Intel Drivers\Applications\Software\Applications\Wave_Embassy_Trust_Suite\EMBASSY Security Center\program files\Wave Systems Corp\EMBASSY Security Center\plugins\cpm.scp\webinterface\ru\js\HelpMessages.js|1229488128.0

.\Drivers\Intel Drivers\Applications\Software\Applications\Wave_Embassy_Trust_Suite\EMBASSY Security Center\program files\Wave Systems Corp\EMBASSY Security Center\plugins\cpm.scp\webinterface\ru\js\Strings.js|1229488128.0

After printing lot of file names successfully, the code fails for one particular file as shown below:

Error:

Traceback (most recent call last):
  File "C:\SyncClientRK\SyncClientRK.py", line 183, in <module>
    SyncClientRK()
  File "C:\SyncClientRK\SyncClientRK.py", line 17, in __init__
    self.getStatus()
  File "C:\SyncClientRK\SyncClientRK.py", line 38, in getStatus
    self.generateLocalStatus()
  File "C:\SyncClientRK\SyncClientRK.py", line 53, in generateLocalStatus
    print(file + "|" + str(os.path.getmtime(file)) + "\n")
  File "C:\Python33\lib\genericpath.py", line 54, in getmtime
    return os.stat(filename).st_mtime
FileNotFoundError: [WinError 3] The system cannot find the path specified: '.\\Drivers\\Intel Drivers\\Applications\\Software\\Applications\\Wave_Embassy_Trust_Suite\\EMBASSY Security Center\\program files\\Wave Systems Corp\\EMBASSY Security Center\\plugins\\cpm.scp\\webinterface\\zh-CHS\\AccessingToolkit.htm'

Please notice that the file is fetched in the loop and is printed, but os.path.getmtime is throwing an error that will not found. Not able to understand why and how to fix this.

like image 504
Romaan Avatar asked Mar 06 '26 21:03

Romaan


1 Answers

That's a 220 character long filename, starting in a local directory. Assuming that the local directory has a path that is longer than 40 characters, you are hitting an old Windows limit of paths that are longer than 260 characters.

Not all ways of handling files in Windows has this limit, but it may be that this is the problem here. If there are filenames in your list that is even longer, then this is clearly not the problem, but that's what I would look into first.

See also: http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx#maxpath

like image 121
Lennart Regebro Avatar answered Mar 08 '26 11:03

Lennart Regebro