I ran across this problem when trying to run code found in this answer to a question about loading Salome from a Python script (Salome is a 3D modeling program). The part of the code relevant to my problem was in creating and re-opening a .txt file. When attempting to open the file, I was getting an error that said there was no such file/directory as that file.
Then I tried just using savetxt() for just some random numpy array (with the directory being my desktop, acheived using os.chdir()), and no file was saved to my desktop, as far as I could tell. Then, to test if the file had been created somewhere without me noticing, I tried using loadtxt() to find it, and I got the same error saying there was no file or directory named MyFile.txt.
Here's my code:
import os
import numpy as np
os.chdir('C:\\Users\\Brahm\\Desktop')
np.savetxt('stuff',npa([7,8]))
np.loadtxt('stuff.txt')
I also tried without quotation marks around stuff in the savetxt line
Is this a bug, or am I doing something incorrectly?
In your program, you are saving your array using -
np.savetxt('stuff',npa([7,8]))
The file name is 'stuff' , not 'stuff.txt' (Please note the difference). Then you are trying to load - np.loadtxt('stuff.txt') . This will not work, because you created file as - stuff , not stuff.txt .
Either store to stuff.txt using -
np.savetxt('stuff.txt',npa([7,8]))
Or load from stuff -
np.loadtxt('stuff')
Related
When I try to write something, such as variables, the code is renamed to the file name on the computer.
For example, if I write:
a = 20
f = 15
print(a+f)
then the code file will automatically be renamed to the first line, i.e. "a = 20"
Then, when I try to run the code, the program outputs nothing but "Python" and some incomprehensible words.
What could it be related to?
enter image description here
enter image description here
I installed the latest version of Visual Stuio Code with Python, they are new, so there should be no problems. But this time it went wrong.
After reinstalling the program, the problem remains.
First of all, if there is no special requirement, please do not use Code Runner to run the script, using the official extension Python is a better choice.
In addition, the dot on your file label means that you have not saved the file, you can add the following setting to enable automatic saving in the settings.
"files.autoSave": "afterDelay",
You may have created the file using the following method. File --> New File... --> Python File. At this time, the file has not been named, also not saved. You can see that there is no such file in the resource manager list at this time.
So the file label shows the first line of codes. This is a feature of vscode, you can refer to this link. And because the file has not been saved, there will be problems executing the script.
You can rename the script file directly (F2), or vscode will remind you to name the file when saving. Another way to create a file is to right click and choose New File..., enter filename and end with .py extension.
im new to pyspark and i want to lunch a pyspark program in standalone cluster, i followed the steps on this tutorial and i lunched my program using this command:
bin\spark-submit examples\src\main\python\LSI_MapReduce\LSI.py
here is the part of my code where the error is happening:
# load the dataset
rows = np.loadtxt('first.txt') <----- here
rows = sc.parallelize(rows)
mat = RowMatrix(rows)
# compute SVD
svd = mat.computeSVD(20, computeU=True)
the first steps of my code did run fine and i got this error:
in the line 200 FileNotFoundError: first.txt not found.
LSI_MapReducefolder has a file named first.txt at the same place as LSI.py
when i run my program on VScode it works perfectly.
how can i fix this error ?
i highely would appriciate any help .
Python, via Numpy, (not Spark) is trying to read the file from where you run your Python interpreter,
The word count example in the link reads the README.md file next to the bin folder, so if that's where you start the command, that's where your file needs to be. Otherwise, cd down into the example folder where your file exists
Also, Spark can read text files or csv files itself; so you shouldn't need numpy to do that
I have been working at this for a while, but I have not been making a ton of progress, and I want to know how to import a python file into another, and with the help of a couple websites and debugging I have gotten to this code: import (__file__ + "\Assets\Minigames\minigame1"). I am getting this error in VSCode: Expected module namePylance Statements must be separated by newlines or semicolonsPylance
Background:
I have three .py files that I use on Spyder every morning:
functions.py file contains a functions I use
calculation.py uses the functions to get the data and do some calculations on it
morning.py uses the data to produce a chart
In calculation.py I have the line to import my functions
%run '/Users/mak/Dropbox/Python/fed nlp/functions.py'
Then I run through around 100 lines of code in calculation.py to use these functions to pull data, clean it, and produce a nice table.
Finally, I open morning.py which uses this table to produce a nice chart for me.
Problem:
In order to speed up my daily process, I tried inserting this line into morning.py:
%run '/Users/mak/Dropbox/Python/fed nlp/calculation.py'
However I get the message:
%run '/Users/mak/Dropbox/Python/fed nlp/calculation.py'
File "/Users/mak/Dropbox/Python/fed nlp/calculation.py", line 2
%run '/Users/mak/Dropbox/Python/fed nlp/functions.py'
^
SyntaxError: invalid syntax
I guess you cannot do a 'run' command via another 'run' command...
Any idea for how I can speed up my daily process?
I'm not sure it's going to work but you could try to put these three files in the same directory and after to write in the "functions" file and "calculation" file:
if "_name_" == "_main_":
pass
After that you have to import these two files in the "morning" file using import:
import functions.py
import calculation.py
In this way you can do everything inside the "morning" file.
I apologize if this does not solve your problem
I am trying to replicate another researcher's findings by using the Python file that he added as a supplement to his paper. It is the first time I am diving into Python, so the error might be extremely simple to fix, yet after two days I haven't still. For context, in the Readme file there's the following instruction:
"To run the script, make sure Python2 is installed. Put all files into one folder designated as “cf_dir”.
In the script I get an error at the following lines:
if __name__ == '__main__':
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
os.chdir(cf_dir)
cf = pd.read_csv(cf_file)
cf_phys = pd.read_csv(cf_phys_file)
ValueError: need more than 0 values to unpack
The "cf_file" and "cf_phys_file" are two major components of all files that are in the one folder named "cf_dir". The "cf_phys_file" relates only to two survey question's (Q22 and Q23), and the "cf_file" includes all other questions 1-21. Now it seems that the code is meant to retrieve those two files from the directory? Only for the "cf_phys_file" the columns 1:4 are needed. The current working directory is already set at the right location.
The path where I located "cf_dir" is as follows:
C:\Users\Marc-Marijn Ossel\Documents\RSM\Thesis\Data\Suitable for ML\Data en Artikelen\Per task Suitability for Machine Learning score readme\cf_dir
Alternative option in readme file,
In the readme file there's this option, but also here I cannot understand how to direct the path to the right location:
"Run the following command in an open terminal (substituting for file names
below): python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile
jobDataFile OESfile
This should generate the data and plots as necessary."
When I run that in "Command Prompt", I get the following error, and I am not sure how to set the working directory correctly.
- python: can't open file 'cfProcessor_AEAPnP.py': [Errno 2] No such file or directory
Thanks for the reading, and I hope there's someone who could help me!
Best regards & stay safe out there during Corona!!
Marc
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
means, the python file expects few arguments when called.
In order to run
python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile jobDataFile OESfile
the command prompt should be in that folder.
So, open command prompt and type
cd path_to_the_folder_where_ur_python_file_is_located
Now, you would have reached the path of the python file.
Also, make sure you give full path in double quotes for the arguments.