Can't load txt file with numpy in VSCode - python-3.x

import numpy as np
dat=np.loadtxt('sample.txt',skiprows=0,dtype=float)
print(dat.shape)
I'm trying to load txt file with numpy in VScode, but it displays such error
dat=np.loadtxt('sample.txt',skiprows=0,dtype=float)
File "C:\Users\asadb\anaconda3\lib\site-packages\numpy\lib\npyio.py", line 961, in loadtxt
fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
File "C:\Users\asadb\anaconda3\lib\site-packages\numpy\lib\_datasource.py", line 195, in open
return ds.open(path, mode, encoding=encoding, newline=newline)
File "C:\Users\asadb\anaconda3\lib\site-packages\numpy\lib\_datasource.py", line 535, in open
raise IOError("%s not found." % path)
OSError: sample.txt not found.
Two files, .py file and .txt file, are in the same folder.

I changed the directory to save .txt file then it moved!! Two files, .py that you want to move and .txt shouldn't be together in the same file or directory.

Assuming the answer you gave yourself is not the answer.
In VSCode you have multiple ways of opening a file. If you open a file directly. It does not always take the directory with it. If you were to load in a textfile. It would not search for it in your .py file directory (Whatever it may be). But it would search in your Python executable or VSCode directory. I recommend navigating to File --> Open Folder. In VSCode. And opening the folder where your .py file is located. This should fix your issue.

Related

FileNotFoundError When Attempting to Open a File in the Same Directory

The txt file is saved in the exact same folder as my code but when I run it I get that traceback. I right clicked saved file directly to folder but when run the code vs studio. I am very new to code sorry for the basic question.
file = open('regex_sum_1114202.txt', 'r')
Traceback:
Traceback (most recent call last):
File "c:\Users\EM2750\Desktop\py4e\ex_11\ex_11.py", line 2, in <module>
file = open('regex_sum_1114202.txt', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'regex_sum_1114202.txt'
screen shot of traceback
Try file = open('./regex_sum_1114202.txt', 'r') instead.
This explicitly specifies that Python should look for the file in the current directory by providing the relative path. Think of the point as a shorthand for the current working directory. So if the current working directory is the directory where the script and the file is, that should work.
Use forward slashes (/) instead of backslashes (\). Backslashes are the default directory separator on Windows, but here they make problems because they are interpreted as escape sequences by Python. Alternatively, you can use two backslashes after another as directory separator: \\.
You can also try to specify the full path before the filename: file = open('c:/Users/EM2750/Desktop/py4e/ex_11/regex_sum_1114202.txt', 'r'). The downside is of course that the path wouldn't be correct anymore if you'd move the file.

I am getting FileNotFoundError: [Errno 2] No such file or directory: 'dna.txt' on Pycharm? Where do I put file to read?

I am trying to open 2 text files on PyCharm but it says file not found. Where do I put the files so they can be found?
I tried moving the files to the folder where all my PyCharm projects are kept but it didn't work.
dna = open('dna.txt', 'r')
dna.close()
dna_results = open("dnaresults.txt", "w")
dna_results.close()
Expected: I don't know honestly, for the text file to open on PyCharm so I can read it?
Actual: FileNotFoundError: [Errno 2] No such file or directory: 'dna.txt'
You need to pass the full path of the file in order to open it, for e.g. if the file is located at /home/john/dna.txt, you need to do.
dna = open('/home/john/dna.txt', 'r')
dna.close()
You can put your file anywhere you want, but you always need to pass the full path of the file in order to access it, otherwise the python interpreter doesn't know where to find it.
As an additional tidbit, if the dna.txt is located in the same folder as where the script is located, dna = open('dna.txt', 'r') will work

FileNotFoundError even though file exist

when trying to open a file using wit open .. getting error that file doesn't exist.
I am trying to parse some txt files , when working localy it works with no issue, but the issue started when I am trying to connect to a network folder. the strange this is that is does see the file , but says its not found .
The Path I referring is '//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs/*' (this folder is full of txt files.
but I am still getting this error:
FileNotFoundError: [Errno 2] No such file or directory: 'Console_log_01-01-2019_08-17-56.txt'
as you see , it does see the needed file .
in order to get to this file I am parsing splitting the path the follwoing way :
readFile = name.split("/")[9].split("\\")[1]
because if I am looking on the list of my files I see them the following way :
['//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs\Console_log_01-01-2019_08-17-56.txt',
after splitting I am getting :
Console_log_01-01-2019_08-17-56.txt
and still it says the file is not found.
def main():
lines =0
path = '//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs/*'
files = glob.glob(path)
print ("files")
print ('\n')
print(files)
for name in glob.glob(path):
print (path)
readFile = name.split("/")[9].split("\\")[1]
print(readFile)
with open(readFile,"r") as file:
lines = file.readlines()
print (lines)
main()
files
['//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs\\Console_log_01-01-2019_08-17-56.txt', '//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs\\Console_log_01-01-2019_08-18-29.txt']
Traceback (most recent call last):
//10.8.4.49/Projects/QASA_BR_TCL_Env_7.2.250/Utils/BR_Env/Call Generator/results/Console_Logs/*
Console_log_01-01-2019_08-17-56.txt
File "C:/Users/markp/.PyCharmEdu2018.3/config/scratches/scratch_3.py", line 19, in <module>
main()
File "C:/Users/markp/.PyCharmEdu2018.3/config/scratches/scratch_3.py", line 16, in main
with open(readFile,"r") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'Console_log_01-01-2019_08-17-56.txt'
Process finished with exit code 1
When you are looking for the file you are looking in the entire path, however when you are opening the file, you are referencing it as if it was in the local path, either change the current working directory with
os.chdir(path)
before opening the file, or in the open statement use
open(os.path.join(path,filename))
I recommend the first approach if you have to open only one file in your program and second if you open multiple files at multiple directories.
In future better format your questions, stack overflow has multiple tools, use them, also you can see how your text looks, make sure to have a look at it before posting. Use the code brackets for your code, that will help whoever is trying to answer.

Why my code cannot find my file/directory

I am trying to get code to read from a .txt file, the file itself is within another file called driving_dataset. The files exist and it should be able to find the .txt file but it won't and I cannot tell what I am doing wrong..
I have triple checked for any spelling errors
#read data.txt
with open("driving_dataset/data.txt") as f:
for line in f:
xs.append("driving_dataset/" + line.split()[0])
It should be directed through driving_dataset folder to the data.txt file but it does not pick it up. and gives me the error
with open("driving_dataset/data.txt","r") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'driving_dataset/data.txt'

How to create a pyd file using command line

I'm just wondering how can i create a .pyd file using python3.5 on win10.By the way I just want to know how to create it using command line like how you create .pyc file.
Something like
f = open("NAMEOFFILE.pyd", "r")
f.close()
should do the trick. this should work with other arbitrary file types. .txt, .json, .py, .whateverfileextensionyouwant

Resources