I try to run this code
import os path_a = r'D:\Prroject' if os.system("cd " + path_a) == 1: print("Invalid Path!") else: print("Valid Path!")
and get ouput
The system cannot find the path specified. Invalid Path!
how to remove the "
The system cannot find the path specified." and make the output only print "
Invalid Path!" .
Redirect the standard error output to /dev/null.
os.system("cd " + path_a + " 2>/dev/null")
This syntax works with bash-type shells; if you use a different shell, consult that shell's manual.
However, you should be aware that running
os.system() is very likely to be useless, as the changed directory only persists for the lifetime of the
os.system() call, which is very short. The next time you run a
os.system() command, you will be back in the original directory.
you should really use the os module for this, here's a potential approach:
try: os.chdir('doesntexist') print('Valid Path!') except: print('Invalid Path!')