pythonsplinter

assert os.path.isfile only working with full path


I've been given an educational assignment during which I write Gherkin scenarios to test a website using Python 3.6, Splinter and Behave. I'm making some pretty good progress but I'm stuck on this little thing. Currently I've succeeded in getting a file to download through a headless instance of Chrome in Ubuntu. However, for the last step of the scenario to pass, I need to verify the file's existence. After a lot of searching I've found a method that works, which is:

assert os.path.isfile('/home/[USERNAME]/Downloads/file.csv')

However, in order to make this test more compatible with other computers, I'd like the path to the file to be shorter and simpler. Most importantly, not using this system's username.

I'm new to all of this so this could very well be a dumb question, but I've been searching all over the place and I simply can't find an answer.


Solution

  • You could rewrite the path using the ~ which replaces /home/[USERNAME]/, so it would become ~/Downloads/file.csv. Then, you could use Python's os.path.expanduser() function as follows:

    assert os.path.isfile(os.path.expanduser('~/Downloads/file.csv'))
    

    os.path.expanderuser() will automatically expand it to /home/[USERNAME]/ for you.