Using an external .dat file with a python script - python

I'm trying to use a python script to read statistics off of a file: savegame.dat
http://pastebin.com/DmsHfPhU is the script
The script came with instructions: "Use: python %script% %savegame.dat%"
I have python, I don't know where to put the savegame.dat file.
For some reason python launcher isn't working so I have to do it manually through the Terminal using the "python" command. I tried putting it in the same folder and it didn't work. I'd like to know what I need to change in the raw paste so it recognizes the savegame.dat. I tried dragging the script into the Terminal.app after opening the python command. With the script saved in the same place as .dat file.

Put it in the same directory as you are running the script in.
Save the paste in the link to a file. Call it foo.py. Put foo.py and the .dat file in the same directory.
Open the terminal.app. cd to the directory that both files are in, then type python foo.py savegame.dat

It doesn't matter where you put the file so long as you specify the full path to it on the command line.

Related

Maya: How do I get the filepath of a script that is loaded into the script editor?

Hi fellow Maya Programmers!
I need some help figuring out a script's filepath from within maya using python. I have a certain script file located somewhere on my harddrive. Let's assume the file is called "C:/Users/Username/Desktop/test.py". In Maya's Script Editor I open the script file using "File" -> "Open Script..." Menu. A new Tab in my script editor appears, displaying the content of my test.py. What I am trying to do now, is run the test.py and print the folder location where that file (test.py) is stored.
Is there anyway to extract that path information?
Thanks in Advance!
No. This does not work like this. You open the script and the script is copied into the maya python script tab. So it is now inside maya, not a textfile any more, even if the content is linked to the file and the file will be updated if you close the tab. What you can do is to import the script with
import yourscriptname as script
print(script.__file__)
To do so your script needs to be somewhere in the pythonpath.

How to get a file from the files on my computer to JupyterLab?

I am very new to this and struggling with the basics. I have a csv file /home/emily/Downloads/Roger-Federer.csv The textbook says that I need to "extract the file and download it to my current directory" on JupyterLab (I am using Python). What does this mean? How do I do this? Thank you
Every running program, including JupyterLab, has a "working directory" which is where it thinks it is on your computer's file system. What exactly this directory is usually depends on how you launched it (e.g., when you run a program from terminal, its working directory is initially the folder your terminal was in when you ran the command, but it's possible for a program to change its own working directory later).
Your file path indicates you're on Linux, so I'd suggest opening a terminal in your JupyterLab and running pwd to have it print out its current directory. (You can also run !pwd in any open notebook if that's easier.) You should then copy your CSV file to that directory.
If you do that, then from your Python code, you can just open the file locally, like open('Roger-Federer.csv') or pandas.read_csv('Roger-Federer.csv'). You don't have to move the file to open it from Python, though, you can just give it the entire file path, like open('/home/emily/Downloads/Roger-Federer.csv'), and that'll work just fine too.

How to make a Python script run without it opening the console window?

I want to double click on my Python script (which uses a Tkinter GUI) and I just want it to open the Tkinter window, not the console window.
To do this, I changed the extension from .py to .pyw and this seems to work fine on Windows but when I double click my .pyw file on a Linux machine, it doesn't work. It simply froze and I had to restart my system.
Please suggest a platform-independent solution that would help me to run the Python script without opening the terminal/command prompt.
it's been a while since i tried on linux, but i believe it should be fairly simple, firstly you need to put a shebang at the top of the script so your shell knows which executable to use:
#!/usr/bin/python
or if you want a specific version you can expand this to:
#!/usr/bin/python3.2
using whichever version you want (only works for first 2 digits)
then you need to mark it as executable:
chmod 711 myfile.py
for more info on this see this page
then when you double click it, on the rpi (last i used linux) it asks if you want to execute it, or execute it in the terminal.
if you choose to execute it without the terminal, you should only see the tkinter GUI
You can use pyinstaller to create the executables for the different platforms you want.
For example,
The syntax of the pyinstaller command is:
pyinstaller [options] script [script ...] | specfile
In the most simple case, set the current directory to the location of your program myscript.py and execute:
pyinstaller myscript.py
PyInstaller analyzes myscript.py and:
Writes myscript.spec in the same folder as the script.
Creates a folder build in the same folder as the script if it does not exist.
Writes some log files and working files in the build folder.
Creates a folder dist in the same folder as the script if it does not exist.
Writes the myscript executable folder in the dist folder.
In the dist folder you find the bundled app you distribute to your users.
Normally you name one script on the command line. If you name more, all are analyzed and included in the output. However, the first script named supplies the name for the spec file and for the executable folder or file. Its code is the first to execute at run-time.
For certain uses you may edit the contents of myscript.spec (described under Using Spec Files). After you do this, you name the spec file to PyInstaller instead of the script:
pyinstaller myscript.spec
You may give a path to the script or spec file, for example
pyinstaller options... ~/myproject/source/myscript.py
or, on Windows,
pyinstaller "C:\Documents and Settings\project\myscript.spec"
pyinstaller

When I try to write to a file using python script in a batch file and then run it. It doesn't write to the file

I have tried writing some content to a file by running a python code in a batch file.I have used the following as the batch command.
python C:\Python27\filtercsv.py
When I tried running the program in IDLE or in the command prompt, it was writing to the file. But, when I tried the same using the batch file, it wasn't writing.
I have included python in the PATH environment variable. So, I guess I don't need to specify the .exe file's location.
Can anyone tell me how to solve this issue?
Do you write the file to an absolute path?
If not you might want to either change the current directory in the batch file before running the script:
pushd C:\Python27
python filtercsv.py
popd
or include a path in the file name you're writing to.
This is because of if you write to a file just by name it will show up in the current working directory, which is not necessarily where the script is located.
(However, you should probably not save your scripts in your Python installation directory, nor write files there because you don't usually have write access there.)

Problems executing python script in notepad++

I have some images that load in my python script.
they reside in c:\Python27\subfolder\images\
in my .py file (which resides in c:\Python27\subfolder\ )
I load them with ./images/file.jpg
It works just fine in IDLE
However when I run the file through notepad++ I get the error:
Failed to load file ./images/file.jpg
How can I fix this with out having to change my actual python code? (It works if I load the images with the full path, but I dont want to do this).
Im using the run command C:\Python27\python.exe "$(FULL_CURRENT_PATH) in notepad++
thank you very much!!
Well to help you fix the problem, you should do this
import os
print os.getcwd() #gets the current working directory
Most likely your problem is that within the IDE, your CWD differs than when you're running it from the console.
If you want it to work like it does in the IDE, you should first get yourself (the console) within that directory, via navigation (os.chdir(...) command).
You could also make a batch/bash file that runs the python script from the directory you need, and call that file from wherever you want (it would still call the python script with the path you gave

Categories