[Tutor] Python working with Bash....arrrggggh!

akleider at sonic.net akleider at sonic.net
Fri Aug 24 06:53:06 CEST 2012


> As I code Python, I find myself falling back on Bash to handle basic OS
> tasks. How do you gurus deal with Python --> Bash conflicts?
>
> For example, if I wish to test if a file exists, I might do
>
> test = Popen('[ -f file-i-want-to-test-for ]')
>
> But the moment I invoke Bash for a test, I must deal with the fact that
> Bash returns a zero for true and a non-zero for false. But in Python,
> zero is false and non-zero is true. So if the file exists, the variable
> 'test' will be zero since that is what was returned by Bash. But if I
> want to test the variable for the existence of the file, I have to test
> the opposite: 'if not test:' because Python sees the zero as False.
>
> Does it become second nature to work with these conflicts? Or do you
> find it more expedient bypass the OS shell and work almost exclusively
> with Python?

try

>>> import os.path
>>> os.path.exists(path)
or
>>> os.path.isfile(file_name)

http://docs.python.org/library/os.path.html


>
>
> Ray
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>




More information about the Tutor mailing list