Solved

python automate script

Posted on 2014-04-10
12
606 Views
Last Modified: 2014-05-07
i have a python script test.py
currently i am using on windows cmd test.py > output.txt

i want another script to auto run test.py every 15 seconds and output file will be random file name, example outpout1.txt, output2.txt

can someone help me to write this script, or if anyone has script pls share with me
0
Comment
Question by:ammadeyy2020
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39992822
I don't understand why you've selected MS SQL and Linux topics. You want to run the script on Windows or Linux? How 'random' do you want the filenames, must they be unique or do you just want a new filename every run?
0
 

Author Comment

by:ammadeyy2020
ID: 39992852
new file name every run
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 39992890
This will run your test.py 100 times:
let a=0
while [ $a -lt 100 ]
do
  test.py > output$a.txt
  let a=$a+1
done ;

Open in new window

0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39992907
You'll want to read up on the sched and subprocess modules. The former should allow you to schedule 15-second intervals; the latter should allow you to execute the other script.

e.g.

import subprocess
import sched
import time

s = sched.scheduler(time.time, time.sleep)

def worker(scheduler, interval):
    with open('output' + str(interval) + '.txt', 'w') as out:
        subprocess.check_call("python test.py", shell=True, stdout=out)
        
    # Restart timer at end of function
    s.enter(15, 1, worker, (scheduler, interval + 1))
    s.run()
    
###  Start timer

s.enter(15, 1, worker, (s, 1))
s.run()

Open in new window


In the above, you basically continually call worker. worker passes itself to the enter method, so you continually execute worker in this fashion. The work gets done in the first two line of worker's body.
0
 

Author Comment

by:ammadeyy2020
ID: 39992910
let a=0
syntax error

pls check attachment
leta.PNG
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39992929
I suggested a Linux script, did you read the all the questions in my first post?
0
 

Author Comment

by:ammadeyy2020
ID: 39992980
./auto.py: line 1: $'\r': command not found
./auto.py: line 2: $'\r': command not found
")syntax error: invalid arithmetic operator (error token is "
./auto.py: line 8: syntax error near unexpected token `done'
./auto.py: line 8: `done ;'

that is the error i get in linux
0
 

Author Comment

by:ammadeyy2020
ID: 39993044
syntax error near unexpected token `done'
 line 8: `done ;'
0
 

Author Comment

by:ammadeyy2020
ID: 39993048
i copied exact samething

let a=0
while [ $a -lt 100 ]
do
  test.py > output$a.txt
  let a=$a+1
done ;

and getting error
 syntax error near unexpected token `done'
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39993134
First, you said:

i want another script...

...which to me the word "another" would imply that you wanted an additional Python script.

Second, you said:

...to auto run test.py every 15 seconds...

Gerwin Jansen's shell script isn't doing that; it's running a fixed number of iterations, one right after the other. So now I'm confused:  What is it that you want?
0
 
LVL 5

Expert Comment

by:Pasha Kravtsov
ID: 40006904
@ammadeyy2020
You need to add
#!/usr/bin/python2

Open in new window

as the first line of your code for python if you run it as an executable :)
you could alternatively use
python2 code.py

Open in new window


and for you bash script (I'm assuming you're doing the same thing) add this to the first line:
#!/bin/bash

Open in new window


when you do ./ the computer doesn't know what kind of language it'll be compiling/running so you need to designate this by using #!/path/to/executable/
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running total between 2 sql tables in Sql 6 43
How to setup virtualization in Redhat? 4 33
Display Date and Time 7 47
SQL Job Failed 6 25
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question