Please check your current directory.

For that just do this:

import os
print (os.getcwd())
Answer from Roman Belousov on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python can't find the file i want to open
r/learnpython on Reddit: Python can't find the file I want to open
March 10, 2021 -

fr = open("alice.txt", "r")
t = fr.read()
fr.close()

Error code:

Traceback (most recent call last):

File "C:/Users/Vincent/PycharmProjects/pythonProject/Python chopping function.py", line 1, in <module>

fr = open("alice.txt", "r")

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

Comment:

I don't know where to paste the file alice.txt. I pasted it in the python folder but it still can't find the file.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python can't find file in directory?
r/learnpython on Reddit: Python can't find file in directory?
October 3, 2020 -

So I wrote a small bit of python that is theoretically supposed to look for numbers and sum them all up in a file.... So I downloaded a file and typed it in the code, but python keeps on not being able to do it: "FileNotFoundError: [Errno 2] No such file or directory: 'NumberExtractSum' "

I didn't make a typo and I tried .txt, which brought me to the question: where should the file be?

If there is another error pls tell me heres the code lol:

import re

lst = list()

file = open(input('Your File:'), 'r')

for line in file :

----y = re.findall('[0-9]+', line)

----for x in y :

--------z = int(x)

--------lst.append(z)

print(sum(list))

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python can't find file in the same directory
python can't find file in the same directory : r/learnpython
May 19, 2021 - Non-absolute paths are relative to the working directory, not the file location. ... What apps can i use to write python?
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python cannot find file even though it exists
r/learnpython on Reddit: Python cannot find file even though it exists
June 13, 2023 -

Hey Everyone,

I am currently trying to write an automation script that does the following:

  1. Log into a website

  2. Download Excel file from website

  3. Put file in folder

  4. Rename file

  5. Format file accordingly

  6. Send file to email list

Currently I'm stuck on step 4 which I feel should be the easiest but I keep running into an error. Long story short is that Python is telling me the file or directory doesnt exist when it does.

Below is my code, thank you to anyone who takes the time to look into this. Also please let me know if you need additional information.

now = datetime.today()

days_since_sunday = (now.weekday()-6) % 7

last_sunday = now -timedelta(days=days_since_sunday)

formatted_date = last_sunday.strftime('%#m/%d/%Y')

file_path = r"C:\Users\adoucette\Desktop\DOT_Weekly\sqlexec.xlsx"

new_file_path = r"C:\Users\adoucette\Desktop\DOT_Weekly\DOT Weekly Sales WE " + formatted_date + ".xlsx"

shutil.move(file_path, new_file_path)

Error is:

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\adoucette\\Desktop\\DOT_Weekly'

EDIT: Thanks for the responses everyone! Truly apprecaite all the help you've given me. i am going to try to make a folder that is not synced to Onedrive to see if that works....stay tuned!

EDIT #2: It was the One Drive folder all along, I created a folder outside of my Desktop so that it wouldnt be synced by One Drive and it worked! Thanks to everyone for commenting and trying to help a newbie like me out.

Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ file location error - [errno 2] no such file or directory. how to fix?
r/learnpython on Reddit: file location error - [Errno 2] No such file or directory. how to fix?
August 20, 2024 -

(SOLVED)

For anyone having the same issue in the future: I got this problem from using commands at the terminal, while following the harvard CS50 course on youtube.

Whenever I tried running python file_name.py through the terminal for different files, I got the error: "can't open file 'C:\\Users\\... : [Errno 2] No such file or directory".

This came up from the code at the 5:22:15 mark, and the cowsay code at the 5:40:03 mark.

1st code:

import sys

if len(sys.argv) < 2:
    print("too few arguments")
elif len(sys.argv) > 2:
    print("too many arguments")
else:
    print("hello, my name is ", sys.argv[1])

2nd code:

import cowsay
import sys

if len(sys.argv) == 2:
    cowsay.cow("hello, " + sys.argv[1])

Solution: the second comment by u/aishiteruyovivi - in layman's terms: you have to change the directory from the shell's root directory to the folder wherein your file is located.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ "file not found" error, but both files are in the same directory.
r/learnpython on Reddit: "File not found" error, but both files are in the same directory.
September 15, 2023 -

Hi,

I'm working on an exercise from Python Crash Course, and I'm getting this error, even though both my program file and the read file are in the same directory/folder: FileNotFoundError: [Errno 2] No such file or directory: 'learning_python.txt'

This is the code i'm running:

from pathlib import Path

print("--- Reading in the entire file:")

path = Path('learning_python.txt')

contents = path.read_text()

print(contents)

print("\n--- Looping over the lines:")

lines = contents.splitlines()

for line in lines:

print(line)

I'm only getting the error when I run the program on VS Code, when I run it in Sublime Text the program runs with no issues.

Does anyone have any suggestions on what might be the problem and how to fix it?

Thanks in advance.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python script in vs code cannot find file which python shell can
r/learnpython on Reddit: Python script in VS Code cannot find file which python shell can
August 20, 2022 -

I am dealing with a pretty mysterious situation.

I have a directory of four files - including one called "iterate.py" and one called "urls".

If I am in this directory and launch a Python shell, the command "json.load(open("urls"))" works perfectly.

However, I wrote that line of code inside the "iterate.py" script, and I am running it in VS Code with breakpoints, in the debugger.

I am repeatedly getting the message that "No such file", on that line of code.

I simply can't understand how this command could work in a Python shell in that directory but not when run from a script in that directory.

I cannot figure out if this is an issue of scope, or if something happens when you run a Python script in the VS Code debugger.

Can anyone please help me?

Thanks very much

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do you link to a local file in a standalone app?
r/learnpython on Reddit: How do you link to a local file in a standalone app?
November 16, 2020 -

On a Mac.

I've been able to use py2app to run a script that includes Pandas.

It basically inputs two files, and outputs one.

My difficulty is that I can't find anywhere how to reference local files. If I hard code it with directory

pd.read_csv("~/test_file.csv"), it only works if I include the file in the build.

Has anyone been able to build something that will search the local directory, then output a file, and make it standalone so that a user doesn't have to know Python or worry about installs?

My stack overflow and GitHub, etc hasn't yielded much luck for Python Pandas Compile, but I'm open to links as well!

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ noob seeks help : running a .py file on windows
r/learnpython on Reddit: Noob seeks help : running a .py file on Windows
August 3, 2021 -

I'm trying to start learning Python again from scratch and I need the smallest amount of help to get started. I did a tiny bit of Python in college, but all the computers were running Linux and all I have now is a Windows 10 laptop. I'm not interested in dual boot or getting another machine just for Linux, I want to understand how to do things on my Windows laptop.

I downloaded Python 3.9.6 again today. I would like to be explained two things.

  1. how do I create a file named "quicktest dot py" (if I write that with .py at the end, it creates a link for whatever reason) that just contains two lines, for example : 'print("hello")' and 'print("bye")'.

I've tried using IDLE but it's impossible to hit "enter" without resetting the damn thing, now I'm just confused how to write a multiple line code. And how to edit it, as well, once you've ran it once. This isn't quite how I remember things from college.

2) how do I execute that file

I can learn all the Python code in the world with online resources, but if I can't find any resource that explains to laymen how to create, save and run a .py file, that's not very helpful, is it? I didn't find much, except jargon-drop pages where I didn't get an explanation I could understand. I have a Master's degree in math, so I hope I can say i'm not a complete idiot, but I'm not exactly a tech-savvy person, or a very computer-literate guy, and I don't code ever. I want to get into it for math purposes though, and it's the most basic things that are in my way right now.

Thanks for your help.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ python giving "file not found" error with both file in same directory and using absolute path
r/learnpython on Reddit: Python giving "file not found" error with both file in same directory and using absolute path
August 6, 2017 -

[Solved] I am an idiot. I had a folder called KanjiDrag, and within that I had the actual source folder kanji_drag which is where the json file and the main module were. The path I was using was accessing the KanjiDrag folder, but not the kanji_drag folder, and I didn't catch the different names. This is my dumbest file IO mistake yet. Thanks for all the replies, many of which I'll still refer to later when I refine this part of my program. Upvotes for everyone!

Hi, all. I'm running into a "no such file or directory" issue in python that I can't figure out.

I'm using windows 7, and I have the file in the same directory as the program I'm running. I got this error, so then I tried using the absolute path (the file is still in the same working directory anyway) and still got the same error. I even checked with os.getcwd and os.path.abspath and copy-pasted the path exactly.

I'm not sure what's going on here. I closed every program that could possibly be running the file. Would I be getting this error if the file is still open in some elusive background program?

This is the relevant bit of code:

print(os.getcwd())
print(os.path.abspath('RainyGenki1&2.json'))

deckName = "C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag\RainyGenki1&2.json"
deck = open(deckName, 'r')   #opens card deck
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Directory not found - Python Help - Discussions on Python.org
March 11, 2023 - I am getting this error: `C:\Users\Anthony.DESKTOP-ES5HL78\AppData\Local\Programs\Python\Python310\python.exe: canโ€™t open file โ€˜C:\Users\Anthony.DESKTOP-ES5HL78\04_02.pyโ€™: [Errno 2] No such file or directoryโ€™ I am sure my directory is correct. What am I missing?
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ questions โ€บ python-can-t-find-local-files-modules
Python can't find local files/modules | DigitalOcean
February 1, 2020 - When running the app inside a virtualenv and trying to run the application using python3.7 application.py I get the following error message: Traceback (most recent call last): File "application.py", line 11, in <module> from config import * ModuleNotFoundError: No module named 'config' What puzzles me is that config.py is located in the same folder as application.py, and not in a subfolder. I have duplicated the setup on my local machine, also running Python 3.7 and inside a virtualenv, and the importing (and the app) works flawlessly.