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

  • Home
  • SEARCH
  • 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 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
import sys sys.path.append('/home/myuser/svn-repos/myproject') from myproject.settings import * But, it says module not found when
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }
import threading event = threading.Event() event.set() print event.wait(1) None event.clear() print event.wait(1) None So
import math def p(n): return 393000*((288200/393000)^n * math.exp(-(288200/393000)))/math.factorial(n) print p(3) When I run it,
I am learning Python - Beautiful Soup by trying to scrape data. I have
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

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.