Python Forum
Group files according to first few characters in filename
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Group files according to first few characters in filename
#4
(Aug-01-2019, 11:32 AM)Friend Wrote: i don't know why this shouldn't work, what exactly didn't work by using 'startswith':

if filename.startswith('...')         # your five letters
Yeah, an example would help understanding what you really need

My directory and file structure is as below
Main_Folder
--SubFolder1
---aaa_001
---bbb_002

--SunFolder2
---aaa_002
---bbb__004

So the idea is I want to group files based on the first 3 characters in this case and have a dictionary structure that will look as below when printing the file_list

{aaa: [aaa_001,aaa_002],
bbb: [bbb_002, bbb_004]}

when using startswith it just gives a true or false value but the result i get is as below:

{aaa_001: [aaa_001],
aaa_002: [aaa_002],
bbb_002: [bbb_002],
bbb_004:[bbb_004]}
Reply


Messages In This Thread
RE: Group files according to first few characters in filename - by python_newbie09 - Aug-01-2019, 06:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Rename part of filename in multiple files atomxkai 7 7,437 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Append files and add column with last part of each filename NiKirk 0 2,649 Feb-04-2022, 07:35 AM
Last Post: NiKirk
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,565 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Rename Multiple files in directory to remove special characters nyawadasi 9 6,515 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,877 May-15-2020, 01:37 PM
Last Post: snippsat
  extract specific data from a group of json-files ledgreve 3 3,323 Dec-05-2019, 07:57 PM
Last Post: ndc85430
  Rename only first 4 characters of filename bmatt8 2 3,888 Nov-15-2018, 05:15 PM
Last Post: nilamo
  copy files from one destination to another by reading filename from csv Prince_Bhatia 3 7,697 Feb-27-2018, 10:56 AM
Last Post: Prince_Bhatia
  How to create def for sorted() from list of versioning files (filename+datetime) DrLove73 10 7,797 Jan-16-2017, 11:43 AM
Last Post: DrLove73

Forum Jump:

User Panel Messages

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