txt: FileNotFoundError: [Errno 2] No such file or directory - python

i have this code:
file_lamma = open("/users/costanzanaldi/scrivania/filelamma.txt",'r')
for linea in file_lamma.readlines():
linea = linea.strip().split(' ')
The Output is:
runfile('/Users/costanzanaldi/Dropbox/Tesi_Naldi/COdice _Python/NUOVO/pannellopiano2.py', wdir='/Users/costanzanaldi/Dropbox/Tesi_Naldi/COdice _Python/NUOVO')
Traceback (most recent call last):
File "<ipython-input-5-c4acfa74cc68>", line 1, in <module>
runfile('/Users/costanzanaldi/Dropbox/Tesi_Naldi/COdice _Python/NUOVO/pannellopiano2.py', wdir='/Users/costanzanaldi/Dropbox/Tesi_Naldi/COdice _Python/NUOVO')
File "/Users/costanzanaldi/anaconda/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Users/costanzanaldi/anaconda/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/Users/costanzanaldi/Dropbox/Tesi_Naldi/COdice _Python/NUOVO/pannellopiano2.py", line 59, in <module>
file_lamma = open("/users/costanzanaldi/scrivania/filelamma.txt",'r')
FileNotFoundError: [Errno 2] No such file or directory: '/users/costanzanaldi/scrivania/filelamma.txt'
The file's name and the path are correct... I don't know what to do
Thank you

I think you should double check the path you provided.
You can access the file's parent folder using terminal, and the use command pwd to get the real path. Then compare it with the one in your code.
And I noticed you use "/users/". Is that correct? On my Mac OSX, it is /Users/. The path is case sensitive.

If this is a Linux or other Unix system you have the letter-case wrong in your open(); it should be Users not users. File and directory names are case-significant in Unix.

Related

No such file or directory: 'GoogleNews-vectors-negative300.bin'

I have this code :
import gensim
filename = 'GoogleNews-vectors-negative300.bin'
model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary=True)
and this is my folder organization thing :
image of my folder tree that shows that the .bin file is in the same directory as the file calling it, the file being ai_functions
But sadly I'm not sure why I'm having an error saying that it can't find it. Btw I checked, I am sure the file is not corrupted. Any thoughts?
Full traceback :
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/run.py", line 1, in <module>
from serv import app
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/__init__.py", line 13, in <module>
from serv import routes
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/routes.py", line 7, in <module>
from serv.ai_functions import checkplagiarism
File "/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/ai_functions.py", line 31, in <module>
model = gensim.models.KeyedVectors.load_word2vec_format(filename, binary=True)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/gensim/models/keyedvectors.py", line 1629, in load_word2vec_format
return _load_word2vec_format(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/gensim/models/keyedvectors.py", line 1955, in _load_word2vec_format
with utils.open(fname, 'rb') as fin:
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/smart_open/smart_open_lib.py", line 188, in open
fobj = _shortcut_open(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/smart_open/smart_open_lib.py", line 361, in _shortcut_open
return _builtin_open(local_path, mode, buffering=buffering, **open_kwargs)
FileNotFoundError: [Errno 2] No such file or directory: 'GoogleNews-vectors-negative300.bin'
The 'current working directory' that the Python process will consider active, and thus will use as the expected location for your plain relative filename GoogleNews-vectors-negative300.bin, will depend on how you launched Flask.
You could print out the directory to be sure – see some ways at How do you properly determine the current script directory? – but I suspect it may just be the /Users/Ile-Maurice/Desktop/Flask/flaskapp/ directory.
If so, you could relatively-reference your file with the path relative to the above directory...
serv/GoogleNews-vectors-negative300.bin
...or you could use a full 'absolute' path...
/Users/Ile-Maurice/Desktop/Flask/flaskapp/serv/GoogleNews-vectors-negative300.bin
...or you could move the file up to its parent directory, so that it is alonside your Flask run.py.

Having issue with subprocess library in python during text extraction from image using OCR

I have been using python OCR code to extract text from the image but i am getting some error. I think error is with subprocess library, however it is built in library. So i couldn't figure out the error exactly. can anyone please help me in resolving this error. My code and error are as below.
OCR code
import os
import tempfile
import subprocess
def ocr(path):
temp = tempfile.NamedTemporaryFile(delete=False)
process = subprocess.Popen(['tesseract', path, temp.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.communicate()
with open(temp.name + '.txt', 'r') as handle:
contents = handle.read()
os.remove(temp.name + '.txt')
os.remove(temp.name)
return contents
str = ocr("C:\\Users\\hp\\Desktop\\MS Thesis\\opencv-text-detection\\opencv-text-detection\\images\\sign.jpg")
print(str)
by executing th above code I got following error
[7]: runfile('C:/Users/hp/Desktop/MS Thesis/python text detection.py', wdir='C:/Users/hp/Desktop/MS Thesis')
Traceback (most recent call last):
File "", line 1, in
runfile('C:/Users/hp/Desktop/MS Thesis/python text detection.py', wdir='C:/Users/hp/Desktop/MS Thesis')
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/hp/Desktop/MS Thesis/python text detection.py", line 26, in
str = ocr("C:\Users\hp\Desktop\MS Thesis\opencv-text-detection\opencv-text-detection\images\sign.jpg")
File "C:/Users/hp/Desktop/MS Thesis/python text detection.py", line 15, in ocr
process = subprocess.Popen(['tesseract', path, temp.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 171, in init
super(SubprocessPopen, self).init(*args, **kwargs)
File "C:\Users\hp\Anaconda3\lib\subprocess.py", line 769, in init
restore_signals, start_new_session)
File "C:\Users\hp\Anaconda3\lib\subprocess.py", line 1172, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
I have been using Python 3.7.1 with anaconda on window 7
Look like there is an error because it cannot found the file
if the file is in the same directory I will recommend you use:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.path.join(BASE_DIR, 'file_dir')

File used by another process error: Python

I am trying to change the contents of a destination file with the help of some source file. When the work is done, I am making sure that I have closed all the files in code (ofcourse, I have made sure that it is not opened in any editor too.)
def write_to_file(self, _source_path, _destination_path):
f_source = open(_source_path, 'r')
f_destination = open(_destination_path, 'r')
f_temp = open(self.temp_path, 'w+')
while source_line or destination_line:...
f_temp.close()
f_destination.close()
f_source.close()
shutil.move(self.temp_path, _destination_path)
return
I am getting below error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/rohit.tayal/PycharmProjects/AutoNVMerge/MainActivity.py", line 151, in <module>
script.write_to_file(source_path, destination_path)
File "C:/Users/rohit.tayal/PycharmProjects/AutoNVMerge/MainActivity.py", line 62, in write_to_file
shutil.move(self.temp_path, _destination_path)
File "C:\Anaconda\envs\AutoNVMerge\lib\shutil.py", line 581, in move
os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\auto_nv_merge.xml'
C:\auto_nv_merge.xml is my temp file.
When working with
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\auto_nv_merge.xml'
you have 2 options, be a hero and try to hunt down the process that is holding that file hostage or just restart your computer.
I usually go with the latter, restart

Access denied when using pydub

I want to use this script to convert a mp3 file to a wav file.
import pydub
from pydub import AudioSegment
pydub.AudioSegment.converter=r"C:\Users\sunha\ffmpeg-4.1-win64-static\bin"
sound = AudioSegment.from_mp3("筷子兄弟 - 小苹果.mp3")
sound.export("筷子兄弟 - 小苹果.wav", format="wav")
But the problem is my access is denied.
Traceback (most recent call last):
File "<ipython-input-1-5faa7bcb6b97>", line 1, in <module>
runfile('C:/Users/sunha/project-001/untitled1.py', wdir='C:/Users/sunha/project-001')
File "E:\program\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "E:\program\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/sunha/project-001/untitled1.py", line 4, in <module>
sound = AudioSegment.from_mp3("筷子兄弟 - 小苹果.mp3")
File "E:\program\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
return cls.from_file(file, 'mp3', parameters=parameters)
File "E:\program\lib\site-packages\pydub\audio_segment.py", line 697, in from_file
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "E:\program\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 143, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)
File "E:\program\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "E:\program\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
PermissionError: [WinError 5] 拒绝访问。
How can I possibly fix the problem?
I tried out multiple solutions, which helped me, as follows:
Rebooting the computer and/or IDE,
Updating Python, IDE, computer,
pip install pydirectory (Command/Anaconda prompt, run as administrator)
Applying read permissions to the file,
Specifying the path to the file, and adding 'r' before the path, to indicate you want to 'read' the audio file, example at the bottom.
I hope this helps you or at least someone else running into this error.
Best of luck.
Source
Example, run in the same folder as the audio file:
from pydub import AudioSegment
import os
os.chmod('audio2.mp3', 777) #You might not need this
sound = AudioSegment.from_mp3(r"D:/audio.mp3")
#Put the 'r' in front of the filepath, to 'read' the audio file.
sound.export("audio.wav", format="wav")

IOError: [Errno 2] No such file or directory: '../data/mnist.pkl.gz'

I'm new to Python and self-teaching myself neural networks from this http://neuralnetworksanddeeplearning.com/chap1.html and I'm having trouble importing the files that I've downloaded for the exercises. This is the error message I keep getting:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
File "mnist_loader.py", line 68, in load_data_wrapper
File "mnist_loader.py", line 42, in load_data
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gzip.py", line 34, in open
return GzipFile(filename, mode, compresslevel)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gzip.py", line 94, in __init__
fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
IOError: [Errno 2] No such file or directory: '../data/mnist.pkl.gz'
I've looked around and tried this:
>>> open('Users/bryanjordan/Documents/neural-networks-and-deep-learning-master/mnist.pkl')
but get this error:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
open('Users/bryanjordan/Documents/neural-networks-and-deep-learning-master/mnist.pkl')
IOError: [Errno 2] No such file or directory: 'Users/bryanjordan/Documents/neural-networks-and-deep-learning-master/mnist.pkl'
I also meet this question. And I find the solution to solve this problem.
If you run the code in the python console, please first use "cd" into the "/src" folder, and run the code.
If you run the code in a python file (like me), you should put the file into the "/src" folder.
It will be solved. The only reason is that the relative path. You should stand in the right place.
Hope to help you.

Categories

Resources