Python Forum
List of pathlib.Paths Not Ordered As Same List of Same String Filenames
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of pathlib.Paths Not Ordered As Same List of Same String Filenames
#4
you should look at: https://pymotw.com/3/pathlib/
a pathlib path is much easier to construct if the path nodes are contained
within a list:
from pathlib import Path

mylocation  = ['..', 'data', 'fipsCodes', 'GNIScodesForNamedPopulatedPlaces-etc', 'CountryNames', 'geonames_20171023', 'Countries.txt']

home = Path('.')
print('\n-- home --')
print(f'{home}')

print(f'{home.name}')
print(f'{home.resolve()}')

print('\n-- mydatapath --')
mydatapath = home.joinpath(*mylocation)
print(f'(\n{mydatapath}')
print(f'({mydatapath.name}')
print(f'{mydatapath.resolve()}')

print('\n-- newdatapath --')
# you can also create a path like
newdatapath = home / 'data'
print(f'\n{newdatapath}')
print(f'{newdatapath.name}')
print(f'{newdatapath.resolve()}')

print('\n-- filelist --')
filelist = [x.name for x in newdatapath.iterdir() if x.is_file()]
print(f'\n{filelist}')

print('\n-- opening files --')
fips_text_file = newdatapath / 'fips.txt'

with fips_text_file.open() as f:
    count = 0
    for line in f:
        line = line.strip()
        count += 1
        print(line)
        if count > 10:
            break
results (part of resolved path removed for security, replaced with ...):
Output:
-- home -- . ... \Tiger\src -- mydatapath -- ( ..\data\fipsCodes\GNIScodesForNamedPopulatedPlaces-etc\CountryNames\geonames_20171023\Countries.txt (Countries.txt ... \Tiger\data\fipsCodes\GNIScodesForNamedPopulatedPlaces-etc\CountryNames\geonames_20171023\Countries.txt -- newdatapath -- data data ... \Tiger\src\data -- filelist -- ['fips.json', 'fips.txt', 'fipsdata.db', 'fipsdataBackup.db', 'FIPSFormat.json', 'FIPSFormat.txt', 'GNIS_CountryFormat.json', 'GNIS_CountryFormat.txt', 'GNIS_DomesticFormat.json', 'GNIS_DomesticFormat.txt'] -- opening files -- { "AmericanIndianAreas": { "data": { "0010": [ "0010", "Acoma Pueblo and Off-Reservation Trust Land" ], "0020": [ "0020", "Agua Caliente Indian Reservation and Off-Reservation Trust Land" ],
Reply


Messages In This Thread
RE: List of pathlib.Paths Not Ordered As Same List of Same String Filenames - by Larz60+ - Nov-11-2017, 07:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 3 523 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  Next/Prev file without loading all filenames WilliamKappler 9 956 Apr-12-2024, 05:13 AM
Last Post: Pedroski55
  Sample random, unique string pairs from a list without repetitions walterwhite 1 601 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  trouble reading string/module from excel as a list popular_dog 0 509 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,433 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  String to List question help James_Thomas 6 1,195 Sep-06-2023, 02:32 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,825 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 1,047 Feb-23-2023, 02:21 PM
Last Post: sparkt
  convert string to float in list jacklee26 6 2,175 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Checking if a string contains all or any elements of a list k1llcod3 1 1,309 Jan-29-2023, 04:34 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020