Access Windows file from Python - python

I am new to Python, and despite my searching, am unable how to properly access a file from a Python Script on Windows with Python 3. I am trying to use Mongosm to import openstreetmap OSM data into mongodb, but get an error when trying to access the file. How can I fix this? Thank you. According to the github instructions, all I need to do is python insert_osm_data.py <OSM filename> (instructions found here)
The error says:
C:\Users\Jusitn>python C:\Users\Jusitn\Desktop\mongosm-master\insert_osm_data
G:\OSM\planet-140430.osm
File "C:\Users\Jusitn\Desktop\mongosm-master\insert_osm_data.py", line 160
print 'node not found: '+ str(node)
SyntaxError: invalid syntax

insert_osm_data.py is intended to be used with Python 2, but you are apparently running it under Python 3. The easiest fix is to install and use a Python 2 interpreter (compared to rewriting the script for Python 3 compatibility).

I myself is a beginner, but the much I am from the error, it looks like there is a syntax error in you file, as you are using python 3 you should use this : print('node not found: '+ str(node)) in line 160 in you file insert_osm_data.py . In python 3 if you print statement is turned into a print() function.

Related

Executing R files from inside Python using PypeR

My current work project is on writing a Python program that must at various points rely on R. Since I don't know R so well, and the person helping me doesn't know Python so well, the actual R code is not in my program. Instead, he opened Notepad, put the R code in there, and saved it as (name).r. When executed, the output is written into a txt file, which Python can then read.
All I have to do is ask Python to ask R to run (name).r
I've tried using subprocess.run. That worked for awhile, and then for some unknown reason stopped working and now does nothing. Then I tried using rpy2, which also worked for awhile; but now it looks like the installation is broken and I'm having trouble getting it reinstalled.
I'd like to give a 3rd option a try now: PypeR. I used pip install pyper. Looked like it was successful.
To keep things simple, I opened Notepad and typed in the following, and saved it as hello.r:
message <- 'goodbye'
write.table(message,'C:/Users/(my name)/Desktop/(folder)/goodbye.txt',row.names=FALSE,col.names=FALSE)
Manually opening R and copy-pasting the lines in one-at-a-time does indeed work. But I'm having trouble getting it to work from Python. Here are some things I've tried (I always put import pyper at the top):
pyper.runR("source('C:/Users/(muy name)/Desktop/(folder)/hello.r')")
This gives NameError: name 'dump_stdout' is not defined
pyper.R("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")
This gives FileNotFoundError: [WinError 2] The system cannot find the file specified
r=pyper.R("C:/Program Files/R/R-3.4.1/bin/i386/Rgui.exe")
r("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")
This causes RGui to open up with a blank R Console. And then nothing happens. When I click back to Python, the console shows Python is busy until I click the halt button, whereupon I get "OSError: [Errno 22] Invalid argument
What is the correct way to execute hello.r?
Thank you
Looks like I got it. This works:
r=pyper.R(RCMD="C:/Program Files/R/R-3.4.1/bin/R")
r.run("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")

TensorFlow Get started page - print first 5 rows

I'm using PyCharm, and when I try to execute the statement from here:
!head -n5 {train_dataset_fp}
IDE complains that this is SyntaxError: invalid syntax and program never executes. I thought the entire tutorial on TensorFlow is in Python, but seems like this code from completely different language. Has anyone proceed successfully through the TensorFlow: Get Started tutorial?
This is not a python command, this is a unix one, to launch the head program.
You can use PyCharm to open a Terminal on your target machine, and type:
head -n5 {train_dataset_fp}
... replacing {train_dataset_fp} with the actual path to your dataset, which you obtained/printed in the previous step of the tutorial, c.f. lines:
train_dataset_fp = tf.keras.utils.get_file(fname=os.path.basename(train_dataset_url),
origin=train_dataset_url)
print("Local copy of the dataset file: {}".format(train_dataset_fp))
Since you're on Windows, you need to use Windows commands to achieve what head would do. If you have Powershell installed, you can use the command gc. If you don't, here's a workaround to print the first 5 lines of file.txt, prefixed with the line number:
findstr /n ".*" file.txt | findstr /b "[1-5]:"
inspired by this answer. Basically it numbers all lines in the file and then picks the first five. Obviously pretty inefficient for large files though. Use the "!" prefix as needed.

Executing Visual Basic Macro (from personal macro workbook) in Python

I'm trying to execute a visual basic script that is located in my personal macro workbook on an excel file that I'm creating. Here's what I have so far:
import os
import win32com.client
df2.to_excel("Apartments.xlsx")
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.open(filename="C:\Users\my\full\path\Apartments.xlsx", ReadOnly=1)
xl.Application.Run("Apartments.xlsx!create_chart.create_chart_proc")
It's throwing an error when opening the excel file on line 5, I have a feeling line 6 won't work either because it comes from my personal macro book. Anyone have ideas on how to get it to function?
PS. my module name is "create_chart" and my macro name is "create_chart_proc"
It would help if you said what error you are getting, and I know that this path
"C:\Users\my\full\path\Apartments.xlsx"
is not really what is in your code. But \ inside a string has a special meaning in Python (for example \n is a newline and \f is a formfeed). Whether your \ is interpreted as you intend depends on the character that comes after it. When you use Windows paths in Python it's generally less error-prone to use a raw string.
r"C:\Users\my\full\path\Apartments.xlsx"
When you get that line to work, if the next line gives trouble, then post that as a separate question.
I got the first part of the code to work simply running the code as follows:
xl.workbooks.open("C:\Users\my\full\path\Apartments.xlsx")
Posted a separate question to get the macro to work correctly.

Playing Audio with subprocess.call in Python

I wanted to play a .wav file, without using external modules, and i read i could do that using this:
def play(audio_file_path):
subprocess.call(["ffplay", "-nodisp", "-autoexit", /Users/me/Downloads/sample.wav])
I however get:
SyntaxError: invalid syntax
If i use os.path.realpath to get the absolute path of the file, i get just the same thing. (The path i see at get info)
Environment is OSX, Python 2.7
Can someone tell me what i am doing wrong? I am new to Python (and to Programming).
There are multiple problems.
Indentation
Code inside the function should be indented, to show that it is part of the function
File name should be in a quotes
It should be a string
It should be:
def play(audio_file_path):
subprocess.call(["ffplay", "-nodisp", "-autoexit", "/Users/me/Downloads/sample.wav"])

Python - L error in declaration

I have tried to use this code: http://www.cgtk.co.uk/data/gemf/generate_efficient_map_file.py to create GEMF file from map tiles. Problem is, when I run this code with specific folder as a parameter, eg. "py generate_efficient_map_file.py Mapnik", I get error on line 6.
File "generate_efficient_map_file.py", line 6 file_size_limit = 2000000000L
And mistake is in word L. How to solve this, when there is a declaration error?
Thx
Try running it like this
python generate_efficient_map_file.py dirname
This works fine for me (no line 6 error - I don't have input data) on python 2.7.3. You can check your python version with
python -V

Categories