0

I'm using Python(3.9) in Pycharm(11.0.10) venv, and I have a question regarding a Pycharm Error: FileNotFoundError: [Errno 2] No such file or directory:.

It's some general questions regarding file path/directory/root thing that has been bugging me for some time.

I've partly solved the problem through some useful answers here; basically the same situation as this (link: 1).

Current attempts(partially solved):

  1. First I tried to add source root folder via [Preferences → Project Structure] and failed. source roots were added but didn't make any difference. (photos attached)

  2. I then edited a path via [Run → Edit configurations → Working directory ]. After this, I can access files within an external folder; which in this case is [textfile.txt] in [chapter4]folder. (photos attached)

Question:

  1. Wondering if there’s a better way to operate?

  2. To be more presice, is there no way to access one particular folder as a whole–in this case, the [Exercise Folder]– that contains several other folders/files (for instance, [chapter3][chapter4],[chapter5])..?

  3. What's the purpose/correct-use of adding source roots in Pycharm..?

Photos:

working version after attempt2

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 1
    Where does this error appear? Is it your code attempting to open the file? Do you supply a file path and it doesn't work? – tdelaney Apr 25 '21 at 16:17
  • so i when i open with `f = open("textfile.txt”)` i get a `FileNotFoundError: [Errno 2] No such file or directory: 'textfile.txt’`. I think the problem was that my current working file is [practice2.py] and its path is `/Users/minhaekim/PycharmProjects/pythonProject/practice2.py”`. While ‘textfile.txt’ is in `/Users/minhaekim/Desktop/CS-ML/linkedIn/Ex_Files_Learning_Python_Upd/Exercise Files/Ch4/textfile.txt` – Minhae_Kim Apr 25 '21 at 16:53
  • I added the latter (ending with `Ch4`) to **[add source root]** from **[Project Structure]** and failed (got the same Error message). After that edited the **[working directory]** of 'practice2.py' by adding the same path following attempt2. After this `f = open("textfile.txt”)` runs without Error. I'm curious if I always have to do what I did manually…? I'm thinking there must be a better way. + Curious what the **[add source root]** does in Pycharm if it isn't doing what I thought it would do? I'm really new to this and quite unfamiliar with the terms themselves... – Minhae_Kim Apr 25 '21 at 17:00
  • Welcome to Stack Overflow. For the file you have to write the whole path (not just the filename). For the modules you need to have them on the PYTHONPATH, which can be done by declaring the folder as source root or explicitly setting sys.path. There are [a countless number of threads about this already](https://stackoverflow.com/search?tab=votes&q=FileNotFoundError%3a%20%20Errno%202%20No%20such%20file%20or%20directory). – bad_coder Apr 26 '21 at 04:45

0 Answers0