473,769 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling another python file within python

i have a python file called pyq which outputs stock quotes, currently i
also have a html file that takes stock ticker inputs, i would like to
bridge the two by building another program that takes the html inputs
and uses them to call the pyq stock ticker program and then output them
into a text file...

any idea how to do this? my tentative code is:

#!/usr/bin/python

import os
import urllib
os.system("pyth on pyq.py ibm > text1.txt")


note: currently the text1.txt outputted is just blank, however if i run
the command normally as 'python pyq.py ibm' in the command line, it
works correctly and gives me the price of ibm.
Mar 14 '06 #1
2 2813
Kevin:
#!/usr/bin/python

import os
import urllib
os.system("pyt hon pyq.py ibm > text1.txt")
Check the return value of os.system.
note: currently the text1.txt outputted is just blank, however if i run
the command normally as 'python pyq.py ibm' in the command line, it
works correctly and gives me the price of ibm.


Same PATH, PYTHONPATH, current working directory, user,... ?

--
René Pijlman
Mar 14 '06 #2
On 2006-03-14, Kevin <ne*******@gmai l.com> wrote:
i have a python file called pyq which outputs stock quotes, currently i
also have a html file that takes stock ticker inputs, i would like to
bridge the two by building another program that takes the html inputs
and uses them to call the pyq stock ticker program and then output them
into a text file...

any idea how to do this? my tentative code is:

#!/usr/bin/python

import os
import urllib
os.system("pyth on pyq.py ibm > text1.txt")
Rather than invoke the shell (which is what system does), you can just
do it all from Python. Something like:

import pyq
pyq.run("ibm")

Then in pyq.py, you could have something like this:

def run(filename):
# do whatever...

def main():
# check args etc..
run(sys.argv[1])

if __name__ == "__main__":
main()

This way pyq would work from the shell if you wanted to run it that way,
and also as a module.

Not quite sure of the details of the input. If the other program is
creating this file ibm which you're immediately reading as soon as it
appears you'd probably be better off with a pipe.

See Python docs for the "socket" module. Or less portably and if you're
on a UNIX system you could use a named pipe (created with mkfifo).
note: currently the text1.txt outputted is just blank, however if i
run the command normally as 'python pyq.py ibm' in the command line,
it works correctly and gives me the price of ibm.


If you launch pyq.py like this it has to be in the current working
directory of the other program. If you just go os.system("pyq. py ibm")
and pyq.py has the #! business at the start of it, then pyq.py has to be
in your PATH. Otherwise it should work. So long as you're sure the file
ibm has been created by this point.

One of the benefits of doing it with import instead (the thing I
described above) is you don't have to worry about the shell and its
environment, or what PATH is, but only the Python environment
(PYTHONPATH, sys.path, that kind of thing). It's more portable.
Mar 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2362
by: Adam | last post by:
Hi I had Ruby 1.6.8-8 for Windows installed on my WinXP Pro machine prior to installing Python 2.3. After installing Python 2.3, I tried to <----- screen output of python interactive command line -----> Python 2.3 (#46, Jul 29 2003, 18:54:32) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from Tkinter import * >>> root = Tk()
7
13009
by: Pankaj | last post by:
The module which i am creating is like Part A: 1. It does some processing by using python code. 2. The result of this python code execution is written to a text file. ] Part B: 1. I read a text file which is outputted by above python script in a C++ program
113
5308
by: John Nagle | last post by:
The major complaint I have about Python is that the packages which connect it to other software components all seem to have serious problems. As long as you don't need to talk to anything outside the Python world, you're fine. But once you do, things go downhill. MySQLdb has version and platform compatibility problems. So does M2Crypto. The built-in SSL support is weak. Even basic sockets don't quite work right; the socket module...
4
6193
by: Quill_Patricia | last post by:
I have a Python script which is used to load data into a database. Up to now this script has been run by customers from the Windows command prompt using "python edg_loader.pyc". Any error messages generated are written to a log file. A project team working in the same company as me here would like to use this loading utility. They write UI applications for Windows using Java. They were able to launch the Python script from within Java by...
1
5776
by: anonymous | last post by:
1 Objective to write little programs to help me learn German. See code after numbered comments. //Thanks in advance for any direction or suggestions. tk 2 Want keyboard answer input, for example: answer_str = raw_input(' Enter answer ') Herr Üü
3
10388
by: Jie | last post by:
Hi all, i'm having trouble executing os.system('source .bashrc') command within python, it always says that source not found and stuff. Any clue? Thanks, Jie
16
3785
by: Jaco Naude | last post by:
Hi there, This is my first post over here and I hope someone can give me some guidance. I'm trying to embed Python into a Visual C++ 2008 application and I'm getting linker problems. I've compiled a DLL of the Python source code using the pythoncode VC++ project in the PCbuild folder of the source download and this works 100% without any warnings etc. I've done this in Debug and Release mode without any problems.
11
5076
by: Emmanouil Angelakis | last post by:
Hi, I am tryiong to do something obviously trivial such as: I have a c program called "tsys2list" that when it is ran it asks the user to give the value of "tcal" which is a variable. I want to call the "tsys2list" from within a pyrthon script lets call it "gamma.py" >>>but<<< I want to pass the value of "tcal" to th eprogram automatically and not by interacting with the keyboard. How do I do that? thanks i advance! manolis
1
2793
by: Martin Rubey | last post by:
Dear all, I'm trying to call from common lisp functions written for Sage (www.sagemath.org), which in turn is written in python. To do so, I tried http://common-lisp.net/project/python-on-lisp/. It was quite easy to get it to run and do some simple things with python. However, I was unable to get sage to run within it. If I start my local python 2.5 and follow (roughly)
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.