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]))
python loops
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.
add a comment |
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]))
python loops
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 ifPST.shpis infilenames. If yesappendthe path. ie,fo.append(os.path.join(dirpath, 'PST.shp'))
– Abdul Niyas P M
18 hours ago
Since Python 3.5globis 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
Seefnmatch.
– 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
add a comment |
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]))
python loops
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
python loops
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.
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 ifPST.shpis infilenames. If yesappendthe path. ie,fo.append(os.path.join(dirpath, 'PST.shp'))
– Abdul Niyas P M
18 hours ago
Since Python 3.5globis 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
Seefnmatch.
– 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
add a comment |
Check ifPST.shpis infilenames. If yesappendthe path. ie,fo.append(os.path.join(dirpath, 'PST.shp'))
– Abdul Niyas P M
18 hours ago
Since Python 3.5globis 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
Seefnmatch.
– 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
add a comment |
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).
What if the filename is uppercase,'PST.SHP'? It would be safer to usefnmatchas 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
add a comment |
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))
add a comment |
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).
What if the filename is uppercase,'PST.SHP'? It would be safer to usefnmatchas 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
add a comment |
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).
What if the filename is uppercase,'PST.SHP'? It would be safer to usefnmatchas 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
add a comment |
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).
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).
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 usefnmatchas 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
add a comment |
What if the filename is uppercase,'PST.SHP'? It would be safer to usefnmatchas 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
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
edited 17 hours ago
answered 18 hours ago
has
674517
674517
add a comment |
add a comment |
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.
user10671234 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Check if
PST.shpis infilenames. If yesappendthe path. ie,fo.append(os.path.join(dirpath, 'PST.shp'))– Abdul Niyas P M
18 hours ago
Since Python 3.5
globis 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