Results 1 to 5 of 5

Thread: beginner Python question

  1. #1
    Join Date
    Nov 2006
    Beans
    36

    Question beginner Python question

    OK, I realize this is probably a newbie question. Long story short, I'm trying to get out of the habit of wistfully reading books on Python and start actually WRITING IT, even if we're talking about painfully simple/basic/beginner scripts.

    What I'm looking for is a way to successfully execute bash within python, I'm just trying to have it check for the presence of a file, if the file exists, move on, if it doesn't create it, which, in bash terms, if I'm not mistaken is


    if [ -f /foo/bar ]
    then
    function
    elif
    (body of program)

    from rooting around in the forums, I gather I need to import os module. Looking at the Python docs, I see allot of options, but no conditional if then loops. Hopefully I'm looking right through it!
    In heaven, all the interesting people are missing. -Nietsche

  2. #2
    Join Date
    Apr 2006
    Beans
    95

    Re: beginner Python question

    from os path import exists

    if exists('Xxx100926.csv'):
    do somehing
    else:
    do something else


    Watch your indentation.

    HTH

    Thorny

  3. #3
    Join Date
    May 2008
    Location
    UK
    Beans
    1,450
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: beginner Python question

    Looking at the Python docs, I see allot of options, but no conditional if then loops. Hopefully I'm looking right through it!
    What do you mean conditional if then loops ??

    If you just mean if/then - you have not been looking in the right place

    Code:
    import os
    
    if os.path.exists("/foo/bar"):
        # Do stuff
    else:
        # Do other stuff
    Python Documentation : http://docs.python.org/index.html

    Python Flow Control (loops/conditionals) : http://docs.python.org/tutorial/controlflow.html
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  4. #4
    Join Date
    Nov 2006
    Beans
    36

    Post Re: beginner Python question

    I spoke poorly. The python docs are understandably deep, I was scratching the wrong tunnels. A little more fussing and I came up with thornmastr's approach.

    import os, os.path

    if os.path.exists("/home/mbean/foobar.py"):
    print ("true")
    else:
    print ("false")

    It works elegantly enough, but it concerns me a little bit in the sense that I see some semi-reliable information that os.path.exists would return True if a file/link had EVER existed, not currently exists...

    Some testing on my own has confirmed that false.
    I'm good, thank you!
    In heaven, all the interesting people are missing. -Nietsche

  5. #5
    Join Date
    Mar 2005
    Beans
    947
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: beginner Python question

    Quote Originally Posted by jadedcritic View Post
    semi-reliable information that os.path.exists would return True if a file/link had EVER existed, not currently exists
    That makes no sense at all. Where are you reading this?

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •