0

Is there any way that you can check if a file exists in the same folder as the program without using:

os.path.isfile or os.path.exists
1
  • 2
    Why would you not use os.path? Commented Feb 23, 2015 at 17:28

2 Answers 2

1

You can still use os.path without 'changing paths all the time', as you outlined in your comment.

import os

def is_file_in_app_path(filename):
    app_path = os.path.dirname(os.path.realpath(__file__))
    file_path = os.path.join(app_path, filename)
    return os.path.exists(file_path)

Fixed as per @tdelaney's comment.

Sign up to request clarification or add additional context in comments.

1 Comment

os.path.join is a platform independent version of '/'.join(...)
0
import os

def openFile(filename):
    try:
        fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
    except:
        print "Nothing here"
        return None
    fobj = os.fdopen(fd, "w")
    return fobj

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.