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 8351535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:39:36+00:00 2026-06-09T08:39:36+00:00

I am trying to read files, inside a folder, reason is the number of

  • 0

I am trying to read files, inside a folder, reason is the number of files inside the folder is not fixed, but if there are 3 text folder, I have to read all the 3 files if 4 all the 4 text files.

Here is the code I’m trying to use, but comes up with an IOError:

for i in os.listdir("./RecordFolder"):
    print i

Output is:
record1.txt
record2.txt

Now the problem is reading the files:

for files in os.listdir("./RecordFolder"):
    filecontent = open(files).readlines()
    for lines in filecontent:
    print lines

Output:

IOError: [Errno 2] No such file or directory: 'record.txt'

Need some help here, thanks

  • 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-09T08:39:37+00:00Added an answer on June 9, 2026 at 8:39 am

    The function os.listdir() only returns the file names, not the full paths, so you should use os.path.join() to add the directory names:

    directory = "./RecordFolder"
    for filename in os.listdir(directory):
        with open(os.path.join(directory, filename)) as f:
            for line in f:
                print line
    

    (Also note that you shouldn’t use file.readlines() for simply iterating over the lines of the files, and that your code fails to close the file. These problems are fixed in the code above.)

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

Sidebar

Related Questions

I am trying to read the files inside a folder, but when I run
I'm trying to read all the files in a folder. If the name is
Im trying to write a program to read a text file through args but
Hi I'm trying to read all files from a given server. What I want
I'm trying to get a value from a variable inside some files, here is
I am trying to read files using Python's ftplib without writing them. Something roughly
I am trying to read multiple files (can be of any format i.e. pdf,
I am trying to read multiple files in R from the working directory and
I am trying to read some smps files with python, and found a fortran
I'm trying to generate POJO's from mapping xml files. I read something about adding

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.