Question

I'm using python and having some trouble reading the properties of a file, when the filename includes non-ASCII characters.

One of the files for example is named:

0-Channel-https∺∯∯services.apps.microsoft.com∯browse∯6.2.9200-1∯615∯Channel.dat

When I run this:

list2 = os.listdir('C:\\Users\\James\\AppData\\Local\\Microsoft\\Windows Store\\Cache Medium IL\\0\\')
for data in list2:
    print os.path.getmtime(data) + '\n'

I get the error:

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: '0-Channel-https???services.apps.microsoft.com?browse?6.2.9200-1?615?Channel.dat'

I assume its caused by the special chars because the code works fine with other file names with only ASCII chars.

Does anyone know of a way to query the filesystem properties of a file named like this?

Était-ce utile?

La solution

If this is python 2.x, its an encoding issue. If you pass a unicode string to os.listdir such as u'C:\\my\\pathname', it will return unicode strings and they should have the non-ascii chars encoded correctly. See Unicode Filenames in the docs.

Quoting the doc:

os.listdir(), which returns filenames, raises an issue: should it return the Unicode version of filenames, or should it return 8-bit strings containing the encoded versions? os.listdir() will do both, depending on whether you provided the directory path as an 8-bit string or a Unicode string. If you pass a Unicode string as the path, filenames will be decoded using the filesystem’s encoding and a list of Unicode strings will be returned, while passing an 8-bit path will return the 8-bit versions of the filenames. For example, assuming the default filesystem encoding is UTF-8, running the following program:

this code should work...

directory_name = u'C:\\Users\\James\\AppData\\Local\\Microsoft\\Windows Store\\Cache Medium IL\\0\\'
list2 = os.listdir(directory_name)
for data in list2:
    print data, os.path.getmtime(os.path.join(directory_name, data))

Autres conseils

As you are in windows you should try with ntpath module instead of os.path

from ntpath import getmtime

As I don't have windows I can't test it. Every os has a different path convention, so, Python provides a specific module for the most common operative systems.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top