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

Which Python condition that check "file A or B or both exist"

May I ask which condition I should use to check the files A or B or both exist then execute the expression A or B or Both?

if or while or try...

enter image description here
Thanks

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

>Solution :

I would test if they both exist first, something like

if fileA exists:
  if fileB exists:
   both_exist = True
  else:
   file_A_exists = True
   
elif fileB exists:
  file_B_exists = True

Of course that code doesn’t actually work, but it gives an idea of where you would want to check for files existing. A good command you can use to check if a file exists is os.path.exists(fileA) but you will need to import os.

And then, if you want to execute the code, all you have to do is run the correct command next to the variable line, so:

Line 3-5:

  both_exist = True
  os.system('python fileA.py')
  os.system('python fileB.py')

That would be for python, and you’d have to import os to actually run the os.system(‘python’) command.

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