Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7745737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:05:51+00:00 2026-06-01T10:05:51+00:00

import os listing = os.listdir(path) for infile in listing: print infile f = open(os.path.join(path,

  • 0
import os
listing = os.listdir(path)
for infile in listing:
    print infile
    f = open(os.path.join(path, infile), 'r')

I have made a script in python that iterates through all files in a directory and opens them. It works ok, the problem arises with the names of some files. The name of the file is Trade_Map_-_List_of_products_exported_by_Côte_d’Ivoire, but when its tries to open it cant I get this error

IOError: [Errno 2] No such file or directory: "C:\\Users\\Borut\\Downloads\\GC downloads\\izvoz\\Trade_Map_-_List_of_products_exported_by_Co^te_d'Ivoire.txt"

The real name has Côte_d’Ivoire in the end, while the name I get when I iterate through listdir has Co^te_d’Ivoire in the end. What is wrong??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:05:53+00:00Added an answer on June 1, 2026 at 10:05 am

    The encoding of os.listdir(path) depends on the encoding of the string path.
    If path is unicode, then the list of entries returned by os.listdir(path) will be unicode. Otherwise, the returned list will use the system default encoding. If you want to be sure to output your list of file correctly, you could try the following (untested):

    import os
    import sys
    
    path = unicode(path, sys.getfilesystemencoding())
    
    # All elements of listing will be in unicode.
    listing = os.listdir(path)
    for infile in listing:
        print infile
    
        # When infile is in unicode, the system to open 
        # the file using the correct encoding for the filename
        f = open(os.path.join(path, infile), 'r')
    

    sys.getfilesystemencoding() is a method to get your system default encoding, which is how open and other methods expect their string inputs to be in (even though unicode is also fine, as they convert them automatically to the default encoding).

    Reference: http://docs.python.org/howto/unicode.html#unicode-filenames

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: import os import csv listing = os.listdir('/directory/my/files/are/in') os.chdir('/directory/my/files/are/in') for
I have a script that creates a directory listing of all pdfs within a
import xlwt import xlrd import os.path import os print os.path.abspath(os.curdir) #Create workbook and worksheet
I would like to build a small python script that basicaly does the reverse
I have a CSV file listing items that I need to store within the
I have noticed that running the program I'm listing below sometimes produces an unwanted
import xlrd book = xlrd.open_workbook(File_1.xls) sheet = book.sheet_by_index(0) print sheet.row_values(0)[0] I am trying to
I have a MySQL file, db.sql . I have tried to import it using:
I have this large txt file 8mb+ that is delimited and I need to
How do I specify the header files in a setup.py script for a Python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.