Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to start random python file with python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

python: can’t open file ‘<randint<1,3>>.py’:

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading