assert os.path.isfile only working with full path











up vote
1
down vote

favorite












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.










share|improve this question







New contributor




Vitz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Did you try to use "os.path.realpath(file)" to get the real path for your file?
    – F.Lira
    Nov 8 at 10:51

















up vote
1
down vote

favorite












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.










share|improve this question







New contributor




Vitz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Did you try to use "os.path.realpath(file)" to get the real path for your file?
    – F.Lira
    Nov 8 at 10:51















up vote
1
down vote

favorite









up vote
1
down vote

favorite











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.










share|improve this question







New contributor




Vitz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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.







python splinter






share|improve this question







New contributor




Vitz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Vitz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Vitz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 8 at 10:42









Vitz

82




82




New contributor




Vitz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Vitz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Vitz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Did you try to use "os.path.realpath(file)" to get the real path for your file?
    – F.Lira
    Nov 8 at 10:51




















  • Did you try to use "os.path.realpath(file)" to get the real path for your file?
    – F.Lira
    Nov 8 at 10:51


















Did you try to use "os.path.realpath(file)" to get the real path for your file?
– F.Lira
Nov 8 at 10:51






Did you try to use "os.path.realpath(file)" to get the real path for your file?
– F.Lira
Nov 8 at 10:51














2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










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.






share|improve this answer

















  • 1




    Thank you! I knew I was very close having tried multiple os.path options, but this I hadn't yet. Works like a charm.
    – Vitz
    Nov 8 at 10:55


















up vote
0
down vote













If you need to get Downloads folder path, than you can use answer to this question: python - Finding the user's "Downloads" folder






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Vitz is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206045%2fassert-os-path-isfile-only-working-with-full-path%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    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.






    share|improve this answer

















    • 1




      Thank you! I knew I was very close having tried multiple os.path options, but this I hadn't yet. Works like a charm.
      – Vitz
      Nov 8 at 10:55















    up vote
    0
    down vote



    accepted










    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.






    share|improve this answer

















    • 1




      Thank you! I knew I was very close having tried multiple os.path options, but this I hadn't yet. Works like a charm.
      – Vitz
      Nov 8 at 10:55













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 10:48









    rugrln

    354




    354








    • 1




      Thank you! I knew I was very close having tried multiple os.path options, but this I hadn't yet. Works like a charm.
      – Vitz
      Nov 8 at 10:55














    • 1




      Thank you! I knew I was very close having tried multiple os.path options, but this I hadn't yet. Works like a charm.
      – Vitz
      Nov 8 at 10:55








    1




    1




    Thank you! I knew I was very close having tried multiple os.path options, but this I hadn't yet. Works like a charm.
    – Vitz
    Nov 8 at 10:55




    Thank you! I knew I was very close having tried multiple os.path options, but this I hadn't yet. Works like a charm.
    – Vitz
    Nov 8 at 10:55












    up vote
    0
    down vote













    If you need to get Downloads folder path, than you can use answer to this question: python - Finding the user's "Downloads" folder






    share|improve this answer

























      up vote
      0
      down vote













      If you need to get Downloads folder path, than you can use answer to this question: python - Finding the user's "Downloads" folder






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        If you need to get Downloads folder path, than you can use answer to this question: python - Finding the user's "Downloads" folder






        share|improve this answer












        If you need to get Downloads folder path, than you can use answer to this question: python - Finding the user's "Downloads" folder







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 10:49









        MihanEntalpo

        1,051918




        1,051918






















            Vitz is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Vitz is a new contributor. Be nice, and check out our Code of Conduct.













            Vitz is a new contributor. Be nice, and check out our Code of Conduct.












            Vitz is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206045%2fassert-os-path-isfile-only-working-with-full-path%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff