run python script with sys.argv on python shell - python

I am a complete novice in python and I have already searched several entries in stackoverflow to help me but I am still struggling. I feel that this should be very easy to do. Your help is greatly appreciated.
I have the following script: https://github.com/openxc/openxc-data-tools/blob/master/tracecsv.py
My goal is to run it on a python shell on window xp to convert Json file like (http://openxcplatform.com.s3.amazonaws.com/traces/nyc/downtown-crosstown.json) in a format that can be saved as csv.
So far, when I run the following code on my python shell (after changing the directory to where my files are located) I get an error and I am not able to have the output csv I am expected:
Here are what I enter in the command shell and the error:
python tracecsv.py downtown-crosstown.json output_file_name
SyntaxError: invalid syntax
tracecsv.py downtown-crosstown.json output_file_name
SyntaxError: invalid syntax
My understanding is that tracecsv.py read the script saved in my specified directory and the next two string specify the input file to be used in the code (as sys.argv1 and sys.argv2). I am expecting a csv file to be saved in my specify directory with my json file converted in excel structure and ready for analysis.
Thank you!

Related

Vision API Web Detection Script

I'm trying to run the sample web detection script (web_detect.py) for the Vision API but am not having any luck. See here for more details on the script.
I've saved the script into a separate python file. I then try to run the file in my python notebook using:
python web_detect.py "http://www.photos-public-domain.com/wp-content/uploads/2011/01/old-vw-bug-and-van.jpg"
... but I get the following error:
File "<ipython-input-15-b3220db6427b>", line 1
python web_detect.py "http://www.photos-public-domain.com/wp-content/uploads/2011/01/old-vw-bug-and-van.jpg"
^
SyntaxError: invalid syntax
See the image below for a screenshot. Any suggestions on what I'm missing and how I can get the file to run?
Thank you!
RE
You need to add an exclamation mark (!) prior to your whole python command so that the notebook will treat it as a command line execution.
I did test on my notebook using your input image and I got the expected response.
Command to run: !python web_detect.py http://www.photos-public-domain.com/wp-content/uploads/2011/01/old-vw-bug-and-van.jpg
Test:

Is it possible to convert an entire PDF file to a bunch images using ImageMagick (Ubuntu, Python)?

I have a Python app which among others converts an entire PDF file to a few jpg.
In Pycharm it is implemented by the code:
import subprocess
subprocess.check_call(["magick",Platforma_IoT.pdf,Platforma_IoT.jpg], shell=True)
That code in Pycharm on Windows 10 works completely fine, it converts every page of a pfd to jpg. But now I want to run that code using Bash console. Unfortunately, I am getting an error in the Bash:
import-im6.q16: unable to open X server `' # error/import.c/ImportImageCommand/358.
./sd.py: line 3: syntax error near unexpected token `["magick","Platforma_IoT.pdf","Platforma_IoT.jpg"],'
./sd.py: line 3: `subprocess.check_call(["magick","Platforma_IoT.pdf","Platforma_IoT.jpg"], shell = True)'
I also tried that code:
import subprocess
subprocess.check_call(["convert","Platforma_IoT.pdf","Platforma_IoT.jpg"])
But I am getting the same error in the bash.
I just want to run code which converts every page of PDF to an image using bash console. How can I do that?
You met a wrong script interpretation. Use shebang to define the right interpretator:
#!/usr/bin/env python3
Or you can launch your script in python directly:
python3 script.py

Having an issue running my python program in VS Code using open()

I am writing a Python program which reads data from a .txt file and writes to it. I am using VS Code as my editor and I am coming across an error where it doesn't recognize infile = open("poem.txt", "r") and tells me there is no such file or directory poem.txt. I can assure you it does exist and it is in the same folder. I can open the same program within IDLE and it runs fine. Is there a way to correct this error for VS Code without trying to hard code in absolute path for the file?
This code is not hardcoded, but will change with the directory the program is run from:
Join constructs file paths from arguments, and getcwd gets the current working directory.
import os
infile = open(os.join(os.getcwd(), "poem.txt"), "r")

Python os.chdir() not changing directory

So, I am following a simple tutorial "Python Tutorial: Automate Parsing and Renaming of Multiple Files" and am already encountering a problem where os.chdir() is not working. I am on a Windows 10 system, running python 3.6, and I have tried using both my regular terminal (which has cygwin installed) and bash on ubuntu on Windows.
Here is the code:
import os
print(os.getcwd())
os.chdir('c:/Users/Michelle Kaiser/Desktop/Lab_Progs/PI3Kalpha')
print(os.getcwd())
Here is the reg terminal:
C:\Users\Michelle Kaiser\Desktop\Lab_Progs>python rename.py
C:\Users\Michelle Kaiser\Desktop\Lab_Progs
C:\Users\Michelle Kaiser\Desktop\Lab_Progs>`
The path that it is returning corresponds to the folder my program is located in. I have moved the program 3 times to verify this. Also, it's obviously returning a path only once, so it's probably not responding to the 2 print statements.
Here is the bash terminal:
mkaiser#ZIPPY:/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs$ python rename.py
/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs
mkaiser#ZIPPY:/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs$
I also tried running the code with os.path.exists(), which did not change the output on either terminal. I have definitely double checked that I am saving my program file from one test to the next. Thanks.
I've been trying to change a file that has no whitespace.
It seems like this person has a similar problem:
Python reading whitespace-separated file lines as separate lines

Error using netCDF4 python module

I am using netCDF4 and python 3.4.
I run the following line of code in order to start writing a new netCDF file that I will be filling with data later in my code following netcdf4 documentation. I however keep getting this error...
File "netCDF4.pyx", line 1466, in netCDF4.Dataset.__init__ (netCDF4.c:19692) RuntimeError: Permission denied
from netCDF4 import Dataset
rootgrp = Dataset('test.nc', 'w', format='NETCDF4')
Any help will be most appreciated.
The "Permission denied" part of the error leads me to believe you don't have permissions to write to the current directory (wherever you are when you run your script).
Check your permissions and/or try giving a full path to put the file in a directory you know you can write to.
dataset.close()
or close ide and delete file 'netCDF4.pyx'
maybe a file exist there, so can't write it.
In my case, this error arises when I am running python by crontab.
The solution is to add following line at the beginning of crontab file.
HDF5_USE_FILE_LOCKING=FALSE

Categories

Resources