Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am making a program that needs a way to create folders, but the only way I found is the OS module. However, it messes with the code that saves and writes to files. Example:
import os
# necessary code that creates folder
os.chdir("Example_directory/")
os.mkdir("New_folder_name")
# necessary code that writes to a text file
file = open("File_directory/document.txt", "w")
file.write("Text sample")
file.close()
And the error message is:
File "C:\Users\First Name Last Name\Desktop\sample\main.py", line 8, in <module>
file = open("File_directory/document.txt", "w")
FileNotFoundError: [Errno 2] No such file or directory: 'File_directory/document.txt'
Even though the directory does exist.
and to prove the os module is the thing messing with the code, when I removed the os module and its block of code, I didn't receive any errors.
My main point is: Does anybody know an alternative way create folders in python?
File_directory presumably exists in the process's original working directory. But you changed its working directory with os.chdir(), and it doesn't exist in Example_directory.
There's no need to use os.chdir() before os.mkdir(), just create it as a subdirectory.
os.mkdir("Example_directory/New_folder_name")
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am trying to load a CSV into a pandas dataframe and I'm getting a "No such file or directory" error.
My project structure is like this:
The function that loads the data from the csv is as such:
def generate_1():
# Read data from CSVs
current_df = pd.read_csv(url_for('static', filename='data/test.csv'))
The error says:
FileNotFoundError: [Errno 2] No such file or directory: '/static/data/test.csv'
And yet we can see the file is on that path in the project directory.
Any ideas what is happening? Thank you.
Its because the application tries to search the CSV file in the root directory of the host machine. To solve it, you might want to consider using relative paths.
./static/data/test.csv # if the file in a static folder inside the execution context
../static/data/test.csv # if the file is in the parent directory
../../static/data/test.csv # if the file is in the grandparent directory
In your case, you'd be interested with:
./static/data/test.csv
or use os.path.relpath:
>>> from os.path import relpath
>>> relpath('./static/data/test.csv')
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Okay so I am very new to Python. Been working on a project, but for whatever reason I get the
"FileNotFoundError: [Errno 2] No such file or directory: 'yt.txt'"
unless in Python I call the extension twice. I'll try to better describe it below.
def mp():
with open("yt.txt", "r") as my_file:
str = my_file.read()
print(str)
mp()
Above is the code. The script won't work unless it's named yt.txt.txt I've tried searching here and other forums but I can't seem to figure it out. The file IS in the same directory as the script, it works with the double extension just not with the single extension. Any help would be appreciated. If you need any additional info just let me know.
I take it you have "show known file extensions" in windows ON so you can confirm the obvious - that its not actually called yt.txt.txt?
[Not too proud to admit stupid stuff like that has tripped me before!]
I'm dumb, thank you to Amiga500 and Barmar for chiming in. The answer was I turned on the "show known file extensions" and it showed the file as yt.txt.txt. I just renamed it, I feel really goofy lol. Thanks all.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to make a script that can delete a file with Python. When I moved the file to my startup directory with my script, a lot of the code was changed, not by me. I've just assumed this was normal and continued trying to make it delete a file on startup. I later realized it wasn't working because one of the call methods kept getting an invalid syntax. Here's my code.
Python Version: 3.8.7
Error message is :
invalid syntax (<unknown>, line 7)
LOAD_CONST(0), LOAD_CONST(None), IMPORT_NAME(os), STORE_NAME(os)
LOAD_CONST(0), LOAD_CONST(None), IMPORT_NAME(shutil), STORE_NAME(shutil)
source = 'C:\\Users\\me\\OneDrive\\Desktop\\startup.py'
destination = 'C:\\Users\\me\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup'
LOAD_NAME(shutil), LOAD_METHOD(move), LOAD_NAME(source), LOAD_NAME(destination), CALL_METHOD[2], STORE_NAME(new_path)
print(new_path)
LOAD_NAME(os), LOAD_METHOD(remove), LOAD_CONST('C:/Users/me/OneDrive/Desktop/delete.txt'), CALL_METHOD[1], POP_TOP
print('File Removed!'), return None
From the code, it's difficult to understand what you are trying to do. If you want to delete a file, you can use the os module in python. For example:
import os
os.remove("/some/file/path/to/remove.txt")
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I cannot understand, how to read file in different dir on Windows.
>>> import os
>>> os.getcwd()
'C:\\Users\\vasyl.v\\AppData\\Local\\Programs\\Python\\Python37'
>>> Fh = open(“d:\\python\\monitor.py”, “r”)
SyntaxError: invalid character in identifier
Can anyone explain me, how to handle Windows paths in Python 3.7.x?
Try this
with open("d:\\python\\monitor.py", "r") as infile:
# do stuff with file here
data = infile.readlines()
in the code above I used what is called a "context manager", this will automatically close the file when the operations are finished. If the code above does not read your file, then either the path is incorrect, file does not exist, or you dont have proper permissions to read the file.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am having a hard time opening a csv with Pandas. I have tried the example in 'Pandas for Everyone' book, have googled many times and last example was here https://analytics4all.org/2016/05/09/python-read-csv-and-excel-with-pandas/
The python program is in a folder called 'lbcsv' then the csv files are in another folder within that one called csv. I have tried not using full path, placing csv file in same folder as the program, then moving them to another folder in the same directory. Just does not seem to want to open the csv. I have tried without the encoding and sep. Before this I had to uninstall pandas and numpy then reinstall because it was giving an error about numpy. I have got around this before but did not ask the question on here so I have no documentation of how I did so.
import pandas as pd
import numpy
servers = pd.read_csv('C:\\Users\\a089673\\Desktop\\lbcsv\\csv\\server.csv', encoding='utf-8', sep=',')
print(servers.head())
Traceback (most recent call last):
File "C:/Users/a089673/Desktop/lbcsv/pandaslb.py", line 4, in <module>
servers = pd.read_csv('C:\\Users\\a089673\\Desktop\\lbcsv\\csv\\server.csv', encoding='utf-8', sep=',')
FileNotFoundError: File b'C:\\Users\\a089673\\Desktop\\lbcsv\\csv\\server.csv' does not exist
For completeness, \a is an escape character and that is causing issue for you. Using raw string as mentioned in the comments solve this issue.
You can see this clearly if you do repr(file_path) . This is one of the gotchas with Windows. I would suggest using forward slashes for accessing files even in Windows to avoid running into these issues.
Can you try below code
df=pd.read_csv('C:\\\\Users\\\\a089673\\\\Desktop\\\\lbcsv\\\\csv\\\\server.csv')
df
Hope it helps !
Answer was to add the 's' at the end of the file name and to use the \ in the file path.