How to "import" scripts into python so it can create them

This is the place for queries that don't fit in any of the other categories.

How to "import" scripts into python so it can create them

Postby QuantumTarantino » Thu Sep 29, 2016 11:15 pm

I have two scripts:

python.py

perl.prl

I want to distribute python.py to all people. The issue is, I don't want them to have to worry about also downloading perl.prl.

As a result, I made a python processor (proc.py ) that accepts perl.prl and takes each line and puts it into a file.write("line") command. It also accounts for string errors by adding backslashes where neccessary.

I can copy and paste every single line of perl.prl written as file.write("line1"); file.write("line2) etc....

and therefore when python.py executes, it can use file= open(filename,'w') in order to CREATE perl.prl temporarily. Once it creates perl.prl it can then run it, and then delete it.

This is all real. And it works. The issue is that it is annoying having to do all this. Does anyone know of an easier way for my python to "absorb" perl.prl so it can create perl.prl within the code?

This way I can avoid people having to download multiple files.

Thanks.
QuantumTarantino
 
Posts: 3
Joined: Tue Aug 02, 2016 4:42 am

Re: How to "import" scripts into python so it can create the

Postby micseydel » Fri Sep 30, 2016 12:40 am

I don't understand, can you provide a code example?
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How to "import" scripts into python so it can create the

Postby ichabod801 » Fri Sep 30, 2016 1:00 am

The usual way to avoid making people download multiple files is to create a zip archive or something similar. If you are insistent on your way of avoiding this problem, you could use a triple quoted string:

Code: Select all
perl = """This is a triple quoted string.
I can span across
multiple lines.
You would put your perl code here."""

perl_file = open('perl.prl')
perl_file.write(perl)
Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801
 
Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: How to "import" scripts into python so it can create the

Postby Ofnuts » Fri Sep 30, 2016 11:37 am

So, you reinvented the "here document" in Python :) But what do you do in a few lines of Perl that you cannot do in Python? And why do you write the command to file when you could be using "perl -e"
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 3 guests