Open Python file only works with Windows folders in English

Asked

Viewed 2,094 times

1

I have a Python program that asks the user for a file path and shows the contents of the file on the screen. The problem is that the code only works when I put the names of Windows folders in English.

For example:

>>> print(open('C:\\Usuários\\usuario\\Documentos\\Gamepobre.txt').read())

returns an error:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Usuários\\usuario\\Documentos\\Gamepobre.txt'

But if I trade Users and Documents for Users and Documents:

>>> print(open('C:\\Users\\usuario\\Documents\\Gamepobre.txt').read())

It shows the contents on the screen as expected.

How will the program use user input, is there any way to convert the path in Portuguese to the English equivalent and avoid this error? I searched but I didn’t find anything that mentions it on the Internet.

I’m sorry if this is incredibly obvious. I’m just getting started. I’m also using Pycharm, if it helps.

2 answers

4


The problem is that the correct, true names of the folders you mentioned are the same English names, Users and Documents.

What happens is that there is a viewing hook set in the hidden files desktop.ini:

[.ShellClassInfo]
LocalizedResourceName=@%SystemRoot%\system32\shell32.dll,-21770
IconResource=%SystemRoot%\system32\imageres.dll,-112
IconFile=%SystemRoot%\system32\shell32.dll
IconIndex=-235

That line LocalizedResourceName calls a function in the shell32.dll exactly at the time of displaying the folder name. This function creates the illusion of a translated name.

That is, in windows explorer and derivatives, an illusory name will be displayed, different from the real name that the folder has. That’s just an illusion and it’s not the real name of the folder.

The most direct way to solve the problem is to use a graphical interface based on windows explorer for the user to select the folder. For example:

from tkinter import filedialog as dlg
path = dlg.askopenfilename()

The above code will display a graphical window for the user to select a file graphically. As this window will use windows explorer internally, the names will appear translated. Even if the user selects the folder Documentos, the function will return Documents, that is, the translation takes place automatically.

  • It worked perfectly! is a much better solution. But just to top it off, there would be some way to translate the folder names of a string if I theoretically decided not to use this solution, or I would have to program everything by hand?

  • In fact I don’t even think that programming by hand is possible @Fupi - the shell32.dll can have any kind of programmed illusion, translations into any language or even other things. The only way to know how a folder will appear "illusorily" for the user who uses windows explorer is by asking for windows explorer itself, since it does not show the real names of the folders. The user is tricked into thinking that the folder has a name, when in fact it has another name. Imagine that your code is running in a Japanese windows - it would have all the translations of all the folders?

0

To get the folder path user in Windows or user in Unix/Linux use the path manipulation function expanduser (https://docs.os.path.html#os.path.expanduser) replacing the ~ by the user folder path.

from os.path import expanduser
print(open(expanduser("~\\Documentos\\Gamepobre.txt").read())
  • I deserved the negative attention. I didn’t take into account the folder documents: The correct would be: import winshell
print winshell.my_documents ()

  • 1

    I’m not the one who said no, but I don’t think your answer answers the question. It is not about getting the path of the document folder, but rather allowing the user to enter a path in Portuguese and have this path translated to be able to use it in functions such as open.

  • @Augustovasques Good afternoon. See this question: Understand the other operators' tendency to only answer about a line break for a variable.

  • @Augustovasques Only that the question covers more things, not yet answered, as: capture of arguments (see "user typed"), and manipulation of substring (new variables: "a string in several substrings so I can work with each of them separately").I ask: can you answer this question? I am interested in the answer/code! zOthank you anyway!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.