os.path.join but extract item of a list











up vote
-1
down vote

favorite












I want to do an os.path.join that contains a list in filenames because there are 3 files in that final folder. I want to only use the PST.shp



import os
fo =
f = r'C:UsersuserDesktopfolderfolder1'
for dirpath,dirnames,filenames in os.walk(f):
print(filenames)
#fo.append(os.path.join(dirpath,filenames))


gives:





['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']



How to filter so I use only the PST.shp full path in the list fo?



something like:



 fo = 
f = r'C:UsersuserDesktopfolderfolder1'
for dirpath,dirnames,filenames in os.walk(f):
if filenames not empty:
print(filenames)
fo.append(os.path.join(dirpath,filenames[0][1]))









share|improve this question







New contributor




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




















  • Check if PST.shp is in filenames. If yes append the path. ie, fo.append(os.path.join(dirpath, 'PST.shp'))
    – Abdul Niyas P M
    18 hours ago












  • Since Python 3.5 glob is recursive so you could use that. See Use a Glob() to find files recursively in Python? which also has pre-3.5 solutions.
    – Peter Wood
    18 hours ago












  • See fnmatch.
    – Peter Wood
    18 hours ago










  • ok this worked but is it possible to do it without referring to the name but use index?
    – user10671234
    18 hours ago










  • @PeterWood Write an answer if you want with fnmatch.
    – user10671234
    18 hours ago















up vote
-1
down vote

favorite












I want to do an os.path.join that contains a list in filenames because there are 3 files in that final folder. I want to only use the PST.shp



import os
fo =
f = r'C:UsersuserDesktopfolderfolder1'
for dirpath,dirnames,filenames in os.walk(f):
print(filenames)
#fo.append(os.path.join(dirpath,filenames))


gives:





['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']



How to filter so I use only the PST.shp full path in the list fo?



something like:



 fo = 
f = r'C:UsersuserDesktopfolderfolder1'
for dirpath,dirnames,filenames in os.walk(f):
if filenames not empty:
print(filenames)
fo.append(os.path.join(dirpath,filenames[0][1]))









share|improve this question







New contributor




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




















  • Check if PST.shp is in filenames. If yes append the path. ie, fo.append(os.path.join(dirpath, 'PST.shp'))
    – Abdul Niyas P M
    18 hours ago












  • Since Python 3.5 glob is recursive so you could use that. See Use a Glob() to find files recursively in Python? which also has pre-3.5 solutions.
    – Peter Wood
    18 hours ago












  • See fnmatch.
    – Peter Wood
    18 hours ago










  • ok this worked but is it possible to do it without referring to the name but use index?
    – user10671234
    18 hours ago










  • @PeterWood Write an answer if you want with fnmatch.
    – user10671234
    18 hours ago













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I want to do an os.path.join that contains a list in filenames because there are 3 files in that final folder. I want to only use the PST.shp



import os
fo =
f = r'C:UsersuserDesktopfolderfolder1'
for dirpath,dirnames,filenames in os.walk(f):
print(filenames)
#fo.append(os.path.join(dirpath,filenames))


gives:





['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']



How to filter so I use only the PST.shp full path in the list fo?



something like:



 fo = 
f = r'C:UsersuserDesktopfolderfolder1'
for dirpath,dirnames,filenames in os.walk(f):
if filenames not empty:
print(filenames)
fo.append(os.path.join(dirpath,filenames[0][1]))









share|improve this question







New contributor




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











I want to do an os.path.join that contains a list in filenames because there are 3 files in that final folder. I want to only use the PST.shp



import os
fo =
f = r'C:UsersuserDesktopfolderfolder1'
for dirpath,dirnames,filenames in os.walk(f):
print(filenames)
#fo.append(os.path.join(dirpath,filenames))


gives:





['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']

['PST.dbf', 'PST.shp', 'PST.shx']



How to filter so I use only the PST.shp full path in the list fo?



something like:



 fo = 
f = r'C:UsersuserDesktopfolderfolder1'
for dirpath,dirnames,filenames in os.walk(f):
if filenames not empty:
print(filenames)
fo.append(os.path.join(dirpath,filenames[0][1]))






python loops






share|improve this question







New contributor




user10671234 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




user10671234 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




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









asked 18 hours ago









user10671234

33




33




New contributor




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





New contributor





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






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












  • Check if PST.shp is in filenames. If yes append the path. ie, fo.append(os.path.join(dirpath, 'PST.shp'))
    – Abdul Niyas P M
    18 hours ago












  • Since Python 3.5 glob is recursive so you could use that. See Use a Glob() to find files recursively in Python? which also has pre-3.5 solutions.
    – Peter Wood
    18 hours ago












  • See fnmatch.
    – Peter Wood
    18 hours ago










  • ok this worked but is it possible to do it without referring to the name but use index?
    – user10671234
    18 hours ago










  • @PeterWood Write an answer if you want with fnmatch.
    – user10671234
    18 hours ago


















  • Check if PST.shp is in filenames. If yes append the path. ie, fo.append(os.path.join(dirpath, 'PST.shp'))
    – Abdul Niyas P M
    18 hours ago












  • Since Python 3.5 glob is recursive so you could use that. See Use a Glob() to find files recursively in Python? which also has pre-3.5 solutions.
    – Peter Wood
    18 hours ago












  • See fnmatch.
    – Peter Wood
    18 hours ago










  • ok this worked but is it possible to do it without referring to the name but use index?
    – user10671234
    18 hours ago










  • @PeterWood Write an answer if you want with fnmatch.
    – user10671234
    18 hours ago
















Check if PST.shp is in filenames. If yes append the path. ie, fo.append(os.path.join(dirpath, 'PST.shp'))
– Abdul Niyas P M
18 hours ago






Check if PST.shp is in filenames. If yes append the path. ie, fo.append(os.path.join(dirpath, 'PST.shp'))
– Abdul Niyas P M
18 hours ago














Since Python 3.5 glob is recursive so you could use that. See Use a Glob() to find files recursively in Python? which also has pre-3.5 solutions.
– Peter Wood
18 hours ago






Since Python 3.5 glob is recursive so you could use that. See Use a Glob() to find files recursively in Python? which also has pre-3.5 solutions.
– Peter Wood
18 hours ago














See fnmatch.
– Peter Wood
18 hours ago




See fnmatch.
– Peter Wood
18 hours ago












ok this worked but is it possible to do it without referring to the name but use index?
– user10671234
18 hours ago




ok this worked but is it possible to do it without referring to the name but use index?
– user10671234
18 hours ago












@PeterWood Write an answer if you want with fnmatch.
– user10671234
18 hours ago




@PeterWood Write an answer if you want with fnmatch.
– user10671234
18 hours ago












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










If you are sure that you will only use the script to process a PST shape, just follow @AbdulNiyasPM's advice and check and process the PST.shp file:



     ...
if filenames not empty:
print(filenames)
if 'PST.shp' in filenames:
fo.append(os.path.join(dirpath, 'PST.shp'))


If you want to keep any shp file, you can simple test that pattern:



     ...
if filenames not empty:
print(filenames)
for name in filenames:
if name.endswith('.shp'):
fo.append(os.path.join(dirpath, name))


Beware: if you need to be able to support non case sensitive platforms like Windows (ie: if you want to accept file names like xx.Shp or yy.SHP) a simple str.endswith is not enough, and you should use fnmatch or at least a non case sensitive regex (thanks to Peter Wood for his remark).






share|improve this answer























  • What if the filename is uppercase, 'PST.SHP'? It would be safer to use fnmatch as it deals with case based on operating system support.
    – Peter Wood
    17 hours ago












  • @PeterWood: Thanks for noticing. Post edited
    – Serge Ballesta
    12 hours ago


















up vote
0
down vote













Use endswith function to check for extensiton as follows,



for dirpath,dirnames,filenames in os.walk(f):  
for f in filenames:
if f.endswith('.shp'):
fo.append(os.path.join(dirpath,f))





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
    });


    }
    });






    user10671234 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%2f53370028%2fos-path-join-but-extract-item-of-a-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    If you are sure that you will only use the script to process a PST shape, just follow @AbdulNiyasPM's advice and check and process the PST.shp file:



         ...
    if filenames not empty:
    print(filenames)
    if 'PST.shp' in filenames:
    fo.append(os.path.join(dirpath, 'PST.shp'))


    If you want to keep any shp file, you can simple test that pattern:



         ...
    if filenames not empty:
    print(filenames)
    for name in filenames:
    if name.endswith('.shp'):
    fo.append(os.path.join(dirpath, name))


    Beware: if you need to be able to support non case sensitive platforms like Windows (ie: if you want to accept file names like xx.Shp or yy.SHP) a simple str.endswith is not enough, and you should use fnmatch or at least a non case sensitive regex (thanks to Peter Wood for his remark).






    share|improve this answer























    • What if the filename is uppercase, 'PST.SHP'? It would be safer to use fnmatch as it deals with case based on operating system support.
      – Peter Wood
      17 hours ago












    • @PeterWood: Thanks for noticing. Post edited
      – Serge Ballesta
      12 hours ago















    up vote
    0
    down vote



    accepted










    If you are sure that you will only use the script to process a PST shape, just follow @AbdulNiyasPM's advice and check and process the PST.shp file:



         ...
    if filenames not empty:
    print(filenames)
    if 'PST.shp' in filenames:
    fo.append(os.path.join(dirpath, 'PST.shp'))


    If you want to keep any shp file, you can simple test that pattern:



         ...
    if filenames not empty:
    print(filenames)
    for name in filenames:
    if name.endswith('.shp'):
    fo.append(os.path.join(dirpath, name))


    Beware: if you need to be able to support non case sensitive platforms like Windows (ie: if you want to accept file names like xx.Shp or yy.SHP) a simple str.endswith is not enough, and you should use fnmatch or at least a non case sensitive regex (thanks to Peter Wood for his remark).






    share|improve this answer























    • What if the filename is uppercase, 'PST.SHP'? It would be safer to use fnmatch as it deals with case based on operating system support.
      – Peter Wood
      17 hours ago












    • @PeterWood: Thanks for noticing. Post edited
      – Serge Ballesta
      12 hours ago













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    If you are sure that you will only use the script to process a PST shape, just follow @AbdulNiyasPM's advice and check and process the PST.shp file:



         ...
    if filenames not empty:
    print(filenames)
    if 'PST.shp' in filenames:
    fo.append(os.path.join(dirpath, 'PST.shp'))


    If you want to keep any shp file, you can simple test that pattern:



         ...
    if filenames not empty:
    print(filenames)
    for name in filenames:
    if name.endswith('.shp'):
    fo.append(os.path.join(dirpath, name))


    Beware: if you need to be able to support non case sensitive platforms like Windows (ie: if you want to accept file names like xx.Shp or yy.SHP) a simple str.endswith is not enough, and you should use fnmatch or at least a non case sensitive regex (thanks to Peter Wood for his remark).






    share|improve this answer














    If you are sure that you will only use the script to process a PST shape, just follow @AbdulNiyasPM's advice and check and process the PST.shp file:



         ...
    if filenames not empty:
    print(filenames)
    if 'PST.shp' in filenames:
    fo.append(os.path.join(dirpath, 'PST.shp'))


    If you want to keep any shp file, you can simple test that pattern:



         ...
    if filenames not empty:
    print(filenames)
    for name in filenames:
    if name.endswith('.shp'):
    fo.append(os.path.join(dirpath, name))


    Beware: if you need to be able to support non case sensitive platforms like Windows (ie: if you want to accept file names like xx.Shp or yy.SHP) a simple str.endswith is not enough, and you should use fnmatch or at least a non case sensitive regex (thanks to Peter Wood for his remark).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 12 hours ago

























    answered 18 hours ago









    Serge Ballesta

    74.5k956128




    74.5k956128












    • What if the filename is uppercase, 'PST.SHP'? It would be safer to use fnmatch as it deals with case based on operating system support.
      – Peter Wood
      17 hours ago












    • @PeterWood: Thanks for noticing. Post edited
      – Serge Ballesta
      12 hours ago


















    • What if the filename is uppercase, 'PST.SHP'? It would be safer to use fnmatch as it deals with case based on operating system support.
      – Peter Wood
      17 hours ago












    • @PeterWood: Thanks for noticing. Post edited
      – Serge Ballesta
      12 hours ago
















    What if the filename is uppercase, 'PST.SHP'? It would be safer to use fnmatch as it deals with case based on operating system support.
    – Peter Wood
    17 hours ago






    What if the filename is uppercase, 'PST.SHP'? It would be safer to use fnmatch as it deals with case based on operating system support.
    – Peter Wood
    17 hours ago














    @PeterWood: Thanks for noticing. Post edited
    – Serge Ballesta
    12 hours ago




    @PeterWood: Thanks for noticing. Post edited
    – Serge Ballesta
    12 hours ago












    up vote
    0
    down vote













    Use endswith function to check for extensiton as follows,



    for dirpath,dirnames,filenames in os.walk(f):  
    for f in filenames:
    if f.endswith('.shp'):
    fo.append(os.path.join(dirpath,f))





    share|improve this answer



























      up vote
      0
      down vote













      Use endswith function to check for extensiton as follows,



      for dirpath,dirnames,filenames in os.walk(f):  
      for f in filenames:
      if f.endswith('.shp'):
      fo.append(os.path.join(dirpath,f))





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Use endswith function to check for extensiton as follows,



        for dirpath,dirnames,filenames in os.walk(f):  
        for f in filenames:
        if f.endswith('.shp'):
        fo.append(os.path.join(dirpath,f))





        share|improve this answer














        Use endswith function to check for extensiton as follows,



        for dirpath,dirnames,filenames in os.walk(f):  
        for f in filenames:
        if f.endswith('.shp'):
        fo.append(os.path.join(dirpath,f))






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 17 hours ago

























        answered 18 hours ago









        has

        674517




        674517






















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










             

            draft saved


            draft discarded


















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













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












            user10671234 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%2f53370028%2fos-path-join-but-extract-item-of-a-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Feedback on college project

            Futebolista

            Albești (Vaslui)