#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Need a little help


    Say i was to create a log so:
    import os
    log = "C:/Log.txt"

    then in a function later on I would have
    open(log, "a+").write(#anything)

    How would I go about deleting this log without knowing the directory, just knowing that it was = log?

    Thanks in advance for any help
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Code:
    >>> f = open('p.py')
    >>> dir(f)
    ['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']
    >>> f.name
    'p.py'
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Nevermind. I figured it out thank you for your help.

    import os
    try:
    if os.path.exists(log):
    os.remove(log)
    except:
    pass
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    How about

    os.remove(log)

    If this advice is still useless please explain carefully, including what parts of the program you have access to and I'll read your post carefully.

    "just knowing that it was = log" is shorthand I apparently don't comprehend.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    so
    import os
    try:
    if os.path.exists(log):
    os.remove(log)
    except:
    pass

    New question..
    How would I put -
    if os.path.exists(log):
    os.remove(log)
    onto one line? Would it be something like
    if os.path.exists(log):return os.remove(log)
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Please test these questions in the interpreter. But yes, I think you're correct.
    >>> 1;2;3;4;45
    1
    2
    3
    4
    45
    >>> if 1:2;3;4;5
    ...
    2
    3
    4
    5
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo