No such file or directory but file exists? - python

I'm writing a python script where I need to open a ".txt" folder and analyse the text in there.
I have saved this ".txt" document in the same folder as my Python script.
But, when I go to open the file; file = open("words.txt",'r')
I get the error: No such file or directory: 'words.txt'.
I don't understand why this is happening?

Maby it's because your current working directory is different from the directory your files are stored. Try giving the full path to the file
file = open("<full_path>\words.txt",'r')

Check if there's a typo in the code or in the filename of the file
Make sure the file is really under the current working directory. Sometimes similar filenames or info shown in your IDE cause confusions
Make sure your are editing the correct script. Sometimes people copy and paste a script into different places and immediately forgot which one they are actually editing
Hope it helps.

It's because the directory of the .py file you are working with is not the same as the .txt file's path.
So, you need to mention the path like this:
file = open("C:/User/Desktop/Folder/words.txt", 'r')

Related

JupyterLab - python open() function results in FileNotFoundError

I am trying to open an existing file in a subfolder of the current working directory. This is my command:
fyle = open('/SPAdes/default/{}'.format(file), 'r')
The filevariable contains the correct filename, the folder structure is correct (working on macOS), and the file exists.
This command, however, results if this error message:
FileNotFoundError: [Errno 2] No such file or directory: [filename]
Does it have anything to do with the way JupyterLab works? How am I supposed to specify the folder srtucture on Jupyter? I am able to create a new file in the current folder, but I am not able to create one in a subfolder of the current one (results in the same error message).
The folder structure is recognized on the same Jupyter notebook by bash commands, but I am somehow not able to access subfolders using python code.
Any idea as to what is wrong with the way I specified the folder structure?
Thanks a lot in advance.
There shouldn’t be a forward slash in front of SPAdes.
Paths starting with a slash exist high up in file hierarchy. You said this is a sub-directory of your current working directory.

Setting work directory parallel to program directory

I have written a program and compiled it which edits CSV files. I don't want to put there a working directory in the code I want the program to edit the CSV files when they are parallel to the program directory.
E.g.:
Path to program: C:\Programs\CSVEditor
Path of CSVs: C:\Programs
But the CSVEditor and the files could be anywhere on the workstation or on a server and I want it to work anywhere...can someone understand what I wanna say?
Does anybody has an idea what I need to do?
Thanks.
Put the CSVEditor script's directory onto your Path. That way, no matter where you are, you can type this at the command line:
csv_editor.py my_file.csv

Where does python open files to on a mac?

I made a simple python script which creates a text file. The contents of the script are
f = open("New", "w")
f.close
Now I moved the script to the desktop (I'm on a mac) and ran it, and nothing shows up. No file is created on the desktop that is visible for me.
I actually made this little script because one of my other scripts involves opening/creating a text file and reading from it. I entered the information wrong while in my program forever, and now the entire thing is broken and I cant fix it because I have no idea where it's created the darn text file. I was under the impression that just opening a file with giving it an absolute path would create it in the same directory as the script. I seem to have been mistaken.
I've been launching the script from the terminal with the command
python3 /Users/me/Desktop/script.py
Because of that, I feel like its creating the file somewhere in the python3 install location or within wherever the python3 unix exec is located. I think. Can't check.
Are any of you guys willing to help out?
-pipsqueaker117
EDIT: Here's a link to the big program which broke.
It'll be created in the current working directory, which is the directory from which you called the script.
Your file will be created in the current directory (most probably in /Users/me/ if you just opened the terminal)
Try:
cd /Users/me/Desktop/
python3 /Users/me/Desktop/script.py
You should modify your program to change the working directory (before you write the file) to the place where you want files to show up.
import os
os.chdir('/Users/me/Desktop') # or whatever
This should be a directory where you have permission to write files.
You can always ask:
import os
cwd=os.getcwd()
print(cwd)
That will print the directory the file (without a path) will be opened in.
You can change to a specific directory (in Python) this way:
import os
try:
os.chdir('/Users/me/Desktop')
except OSError as e:
print e

How to open a file inside a folder which is in the same folder as a Python program?

Basically I am looking for a simple way to open text file in a folder that is inside the same folder as the program.
My directory structure looks like this:
/programfolder/textfiles/textfile
And I'm trying to use open like this:
text=functionthatgetsfilename()
file=open("textfiles/"+text,"r")
What am I doing wrong? Do I just have a typo somewhere?
You need to know the difference between the Current Directory and the directory your script is in. Your current directory is the directory that you started the application from, in the command line (CMD, SH, etc). You can show that with os.path.normpath(os.curdir).
To solve your problem, you can use
file=open(os.path.join(os.path.dirname(__file__),'holdstextfiles',text),'r')
or
os.chdir(os.path.dirname(__file__))
...
The first solution uses the absolute path to your desired file, which is the same no matter what: it's absolute
The second solution changes the current directory before trying to use the relative path that you're using.

Opening/running Excel file from python

I need to start excel and open a file directly from python. Currently I am using:
import os
os.system('start excel.exe file.xls')
However I do not get desired result. I want to open a file from local destination (file is in the same folder with program), but this code open's the file with same name in my home (user) directory and not from my program directory.
The problem is that the directory where the program is located is not used. The current working directory is. So you have to find out which directory your program is located in, which python conveniently prepared for you in:
sys.path[0]
and either change directory to it:
os.chdir(sys.path[0])
or give full path for the file you want to open
os.system('start excel.exe "%s\\file.xls"' % (sys.path[0], ))
Note, that while Windows generally accept forward slash as directory separator, the command shell (cmd.exe) does not, so backslash has to be used here. start is Windows-specific, so it's not big problem to hardcode it here. More importantly note that Windows don't allow " in file-names, so the quoting here is actually going to work (quoting is needed, because the path is quite likely to contain space on Windows), but it's bad idea to quote like this in general!
You can also define the directory, where the python should operate.
import os
os.chdir('C:\\my_folder\\subfolder')
os.system('start excel.exe my_workbook.xlsx')
Don't forget to use backslashes in your path and there must be two of them everytime.
my_workbook.xlxs - here will be the name of your file
That file must be in that folder :)

Categories