Python os function not working as intended? - python

Am I missing something here, I am attempting to make a directory then change CWD into that directory, however it keeps throwing an error saying "FileNotFoundError: [Errno 2] No such file or directory:". This only happens if I first remove the directory and start with no directory there, once its created the first time it works fine.
If I run the code twice it works, first time I get the error.
import os
def func_name_here():
os.popen('mkdir -p /tmp/tun')
os.chdir(r'/tmp/tun')
func_name_here()
This gives me...
Traceback (most recent call last):
File "test.py", line 9, in <module>
func_name_here()
File "test.py", line 6, in func_name_here
os.chdir(r'/tmp/tun')
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tun'

Related

Python - [Errno 13] Permission denied. Not able to open the file

I'm getting IOError: [Errno 13] Permission denied and I don't know what is wrong wit this code.
I'm trying to read a file given an absolute path
with open(r"E:\Paper\codedata.tsv", 'r+') as temp_f:
Error:
Traceback (most recent call last):
File "E:\Paper\code.py", line 18, in <module>
with open(r"E:\Paper\codedata.tsv", 'r') as temp_f:
What I have tried so far:
Changed file permissions. I am using Windows 10
Windows is upto Date
Working in Anaconda spyder. Tried opening spyder in admin and running the code
Changed directory of data to different path
I am using Python 3.

Having an issue running Python Code in terminal

Hi everyone so I have some python code I am trying to run from my terminal (will not work in an IDE) to take a .mp3 file and classify the genre of the song based on the Spectrogram that we are using the librosa library plugin for. The code is from this gitHub: https://github.com/cetinsamet/music-genre-classification .When I use the command prompt specified by the gitHub user who created this app I get this error in my terminal:
(base) Nicos-MacBook-Pro:src nico$ python3 get_genre.py ../test.mp3
Traceback (most recent call last):
File "get_genre.py", line 61, in <module>
main(sys.argv[1:])
File "get_genre.py", line 30, in main
net.load_state_dict(torch.load(MODELPATH, map_location='cpu'))
File "/Users/nico/opt/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 419, in load
f = open(f, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '../utils/net.pt'
Here is the command line specified: $ python3 get_genre.py ../test.mp3
The error message quite plainly tells you that the code depends on having a file ../utils/net.pt

Python numpy.savetxt - no such file or directory, although path is correct

Found no solution here on stackoverflow yet. In the threads I read, where this problem is considered, the problem was triggered by a wrong path. But in my case, the path should be correct, since I copied it to the windows navigation line and it worked.
The error message looks as follows:
Traceback (most recent call last):
File "C:\Users\micha\Dropbox\10. Semester\Fabios_error_bound\python_new_pc\feb_error_calculation_and_plotting\feb_error_calculation_and_plotting\feb_error_calculation_and_plotting.py", line 99, in <module>
np.savetxt(os.path.join(working_directory,error_folder,current_name.replace('.txt','')+'error_pop11.txt'),error_pop11, fmt='%20.14f')
File "C:\Python27amd64\lib\site-packages\numpy\lib\npyio.py", line 1194, in savetxt
fh = open(fname, 'w')
IOError: [Errno 2] No such file or directory: 'C:\\Users\\micha\\Dropbox\\10. Semester\\Fabios_error_bound\\python_new_pc\\feb_error_calculation_and_plotting\\feb_error_calculation_and_plotting\\error2\\ses_pop_site_eb__ohmic_lam50_gam100_T100_SD_ohmic_lam50_gam100_T100_angle39_Chl658_Egap0_J100_M2_K0_L8error_pop11.txt'
Furthermore, it works on my PC, but not on my notebook. In both cases I'm using Visual Studio with Python 3.5 and 3.6, respectively (with 2.7 it also doesn't work on my Notebook).
Does anybody has a clue?

Opening a file in Python gives me errors

I am quite new to Python and I am having problems opening a file in Python.
I want to open a text file called 'coolStuff' in a Folder on my Desktop and this is how I type in the command but I still get an error message. The file exists and so I do not understand why I get that error message.
open("coolStuff.txt","r")
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
open("coolStuff.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'coolStuff.txt'
If you want to simply supply a filename like coolStuff.txt without also providing a full directory, then you have to make sure that Python is running in the same directory as the file. If you aren't sure what directory Python is running in, try this:
import os
print(os.getcwd())
You have two options:
let's say your file is in C:\path\to\dir\coolStuff.txt
1.
open(r'C:\path\to\dir\coolStuff.txt','r')
2.
import os
os.chdir(r'c:\path\to\dir')
open('coolStuff.txt', 'r')
Because the file you want to open is not in the current directory.You can find the file 'coolStuff.txt' in the terminal and launch your python environment at the same directory.

Unable to get a pdf file from executing pyreport

We have been asked to generate a report using pyreport, but anytime I run the command pyreport last.py in command prompt I get these feedback:
Traceback (most recent call last):
File "C:\Python27\Scripts\pyreport-script.py", line 9, in <module>
load_entry_point('pyreport==0.3.4c', 'console_scripts', 'pyreport')()
File "C:\Python27\lib\site-packages\pyreport-0.3.4c-py2.7.egg\pyreport\pyreport.py", line 52, in commandline_call
pyfile = open(args[0],"r")
IOError: [Errno 2] No such file or directory: 'last.py'
Please help me solve this problem. i also already have latex installed on my computer does it have anything to do with this problem?

Categories