Search code examples
pythonutf-8chdir

chdir modifying the path in Python


I've got a program that reads strings with special characters (used in spanish) from a file. I then use chdir to change to a directory which its name is the string.

For example in a file called "names.txt" I got the following

Tableta  
Música
.
.
etc

That file is encoded in utf-8 so that I read it from python as follows

f=open("names.txt","r",encoding="utf-8")
names=f.readlines()
f.close()

And it does read all successfully

print(names)

output:

['Tableta\n','Música\n', ...etc]

Problem arises when I want to change to the first directory (the first name 'Tableta',without the newline character)

chdir(names[0][:-1])

I get the following error

FileNotFoundError: [WinError 2] The system cannot find the file specified: "\ufeffTableta"

And it does only happen with the first name, which was very odd to me. With other names it is able to change directories whether they have special characters or not

I supposed it had to do something with the encoding, because of that '\ufeff' extra added character. So I changed the "names.txt" file to ANSI coding and deleted all the special characters so that I could read it with python, and it worked. But thing is that I need to have that file in utf-8 coding so that I can read special characters. Is there any way I can fix this? Why is python adding that '\ufeff' character to the string and only with the first name?


Solution

  • Your file "names.txt" has a Byte-Order Mask (BOM). To remove it, open the file with the following decoder:

    f = open("names.txt", encoding="utf-8-sig")
    

    As a side note, it is safer to strip a file name: names[0].strip() instead of names[0][:-1].