0

I have again problems with that famous Exception. The problem is the following: I have wanted to write a simple code for an easy exercise about files and excel libraries (openpyxl) that I have to be make for my python programming course. At first, I have to say that I usually make all my codes in iOS. For this time, I have wanted to do this exercise in my old PC whose O.S. is Windows, so I have exported the file where I work in iOS in a pen drive and next, export that file in my Windows desktop. I opened VSC and the first code's line is the problem because I try to load the excel file but the exception FileNotFoundError appears.

As simple as write:

import openpyxl
fw = openpyxl.load_workbook("Data_File_2.xlsx")

The file when I'm writing the code is in the follow route: C:\Users\xxxx\Desktop\pyWork which is the same route where the file "Data_File_2" is in it. Well, the problem continues because every time I want to write a new code for working with extern files, this Exception appears again, even with other 'py' files that I made where I worked with extern files and now I can't run because this Exception appears again and block the program. (I'm talking about old files made in previously days or weeks which works correctly with external files) For example, now I'm trying to open a try file .txt from a new py.file, both of them located in the same directory:

with open("try_to_open.txt","r") as op_file:
    rfile = op_file.read()
    print(rfile)

#The exception FileNotFoundError appears again in the first line.

I know that I can use try/else blocks but I want to know why this Exception appears, what is the Error in this case and why it appears every time I want to work with external file one time that the Exception appears at first.

Pac0
  • 21,465
  • 8
  • 65
  • 74
molik
  • 15
  • 4
  • 3
    _For example, now I'm trying to open a try file .txt from a new py.file, both of them located in the same directory_ It doesn't matter that they're in the same directory. `open()` is looking in the **current** directory, which is not the same. – John Gordon Jul 30 '22 at 01:15
  • You can use print(os.getcwd()) (import os) to get the current working directory and/or use the absolute path if your're still not sure. You can even go further and check if the file exists in the expected directory. – moken Jul 30 '22 at 08:56
  • You can try: **import sys** and then sys.path.insert(1, "") Remember to change \ to / of your absolute path. I think with this you should be fine to go, else you would have to prove some clear code which I think with that someone can offer a better solution. – Jamiu S. Jul 30 '22 at 09:19
  • Hi @moken I've used os.getcwd() but as I said, the results in that print is a route which ends in the directory where the files are in. However, the exception still appearing when I run the programm, I dont know why. Inside the last directory of this route are the .xlsx and .py files but when I try to load (openpyxl.load_workbook("datafile.xlsx") the excel file in VSC the exception FileNotFoundError appears. – molik Aug 03 '22 at 09:59

1 Answers1

0

I think I have found a solution but I dont know if it's correct for similar situations like this. I was able to resolve by writing as follows:

import openpyxl

file_work = openpyxl.load_workbook("Pyworks\\Data_File_2.xlsx")
lemon
  • 14,875
  • 6
  • 18
  • 38
molik
  • 15
  • 4