Sign Up

Have an account? Sign In Now

Sign In

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

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 642
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 2
Kenil Vasani
Asked: December 14, 20202020-12-14T21:08:17+00:00 2020-12-14T21:08:17+00:00In: Python

Python error: FileNotFoundError: [Errno 2] No such file or directory

  • 2

I am trying to open the file from folder and read it but it’s not locating it. I am using Python3

Here is my code:

import os
import glob

prefix_path = "C:/Users/mpotd/Documents/GitHub/Python-Sample-                
codes/Mayur_Python_code/Question/wx_data/"
target_path = open('MissingPrcpData.txt', 'w')
file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if 
f.endswith('.txt')]
file_array.sort() # file is sorted list

for f_obj in range(len(file_array)):
     file = os.path.abspath(file_array[f_obj])
     join_file = os.path.join(prefix_path, file) #whole file path

for filename in file_array:
     log = open(filename, 'r')#<---- Error is here

Error: FileNotFoundError: [Errno 2] No such file or directory: 'USC00110072.txt'

filepythonpython-3.x
  • 1 1 Answer
  • 11 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      You are not giving the full path to a file to the open(), just its name.

      You would have to either os.path.join() correct directory path to it, or os.chdir() to the directory that the files reside in.

      From your code I can deduce though, that you are forgetting to modify the the file_array list. To fix this, change the first loop to this:

      file_array = [os.path.join(prefix_path, name) for name in file_array]
      

      Also, remember that os.path.abspath() can’t deduce the full path to a file just by it’s name.


      Let me reiterate.

      This line in your code:

      file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if f.endswith('.txt')]
      

      is wrong. It will not give you a list with correct absolute paths. What you should’ve done is:

      import os
      import glob
      
      prefix_path = ("C:/Users/mpotd/Documents/GitHub/Python-Sample-"    
                     "codes/Mayur_Python_code/Question/wx_data/")
      target_path = open('MissingPrcpData.txt', 'w')
      file_array = [f for f in os.listdir(prefix_path) if f.endswith('.txt')]
      file_array.sort() # file is sorted list
      
      file_array = [os.path.join(prefix_path, name) for name in file_array]
      
      for filename in file_array:
           log = open(filename, 'r')
      
      • 9
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      Python, Error while installing matplotlib

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com