1

hi i build path file from a csv file information but in open file error

No such file or directory: 'C:\\New folder\\a\\\xef\xbb\xbf90305.xlsx'

when i print filename show

C:\New folder\a\90305.xlsx

please help

 with open (r'C:\New folder\li.csv','rb')as csvfile:
            spamreader=csv.reader(csvfile,delimiter=',')
            for row in spamreader:
                filename = r'C:\New folder\a'
                suffix='.xlsx'
                filename=os.path.join(filename,row[0]+suffix)
                with open(filename,"rb")as fo:
                    print fo
NPE
  • 486,780
  • 108
  • 951
  • 1,012
mrgis
  • 13
  • 3

2 Answers2

0

The filename prefix, row[0], is UTF-8 encoded and starts with the Unicode byte order mark (BOM).

You can use the following to decode it:

prefix = row[0].decode('utf-8-sig')
filename = os.path.join(filename, prefix + suffix)

To read more about encodings, see 7.8.2. Encodings and Unicode in the manual.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Looks like there are some encoding issues going on. The data from the CSV file is encoded, so open the file using the codecs module (Python 2) or specify the encoding when you open the file (Python 3). Assuming UTF8 encoding, and using the utf-8-sig codec to take care of any byte order marker (BOM) at the start of the file:

Python 2

import codecs

with codecs.open(r'C:\New folder\li.csv','rb', encoding='utf-8-sig') as csvfile:
    etc.

Python 3

with open(r'C:\New folder\li.csv','rb', encoding='utf-8-sig') as csvfile:
    etc.

This assumes that there is only one UTF8 BOM and that it occurs at the beginning of the file, not at random points in the file. You are using windows, so that is not a bad assumption.

mhawke
  • 84,695
  • 9
  • 117
  • 138