How to start random python file with python
Question:
I have 10 python file (1.py, 2.py, 3.py …. 10.py) i want to start random with python script, with while true. For ex. script start random py file (4.py) after finish the commands start again new random file (1.py) I want it to spin like this forever.
I try with this command but not working.
while True :
p = subprocess.Popen(['python', 'randint(1,10).py'])
Script give error
python: can’t open file ‘<randint<1,3>>.py’:
Answers:
Try
while True :
p = subprocess.Popen(['python', str(randint(1,10))+'.py'])
randint(1,3)
is a function, not a string, so it should be outside ''
s. Then, I use str()
to convert the number returned by randint
to a string. Finally, I added +
to concatenate str(randint(1,3))
and '.py'
! Also, if you have 10 files, then the second parameter of randint
should be 10.
I have 10 python file (1.py, 2.py, 3.py …. 10.py) i want to start random with python script, with while true. For ex. script start random py file (4.py) after finish the commands start again new random file (1.py) I want it to spin like this forever.
I try with this command but not working.
while True :
p = subprocess.Popen(['python', 'randint(1,10).py'])
Script give error
python: can’t open file ‘<randint<1,3>>.py’:
Try
while True :
p = subprocess.Popen(['python', str(randint(1,10))+'.py'])
randint(1,3)
is a function, not a string, so it should be outside ''
s. Then, I use str()
to convert the number returned by randint
to a string. Finally, I added +
to concatenate str(randint(1,3))
and '.py'
! Also, if you have 10 files, then the second parameter of randint
should be 10.