-3

I have a folder with 10 files excel files inside, the files end with the name 'AA.xlsx', 'BB.xlsx' etc I need a code to get the last last two letters and put this into a list, and I need it for all 10 files.

So something like this; [AA, BB, CC, CD, HN...]

Also, the files paths are like this ;

'2019-10-02_xxx_2018-12-31_GB00Bvvvv_Hooooooooooooxxxxxxxxxxxyyyyn_30163-000-HN.xlsx'

italy
  • 155
  • 6
  • 3
    Possible duplicate of [How do I list all files of a directory?](https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory) – sniperd Oct 08 '19 at 13:26

3 Answers3

0

Something like:

import os

path = "C:/path/to/folder"

for root, dir, files in os.walk(path, topdown=False):
    codes = [name[-7:-5] for name in files]
    # [-7:-5] will only work of all files have '.xlsx' as extension.
0
import os
files = [file[-7:-6] for file in os.listdir("<path>")]

https://www.tutorialspoint.com/python/os_listdir.htm

clubby789
  • 2,543
  • 4
  • 16
  • 32
  • This works, but not all the files are the same length. so not all last two characters are in the list – italy Oct 08 '19 at 13:37
0

I'd probably use re for this, and just match all the .xlsx files

import os
import re

FOLDER_DIR = '<path to the folder that contains your files>'
ENDING_PATTERN = r'([A-Z]{2}).xlsx$'

file_endings = []

for _, _, files in os.walk(FOLDER_DIR):
    for fileName in files:
        matches = re.findall(ENDING_PATTERN, fileName)
        if matches:
            file_endings.append(matches[0])

print(file_endings)

This uses a regex pattern ([A-Z]{2}).xlsx$ which matches all .xlsx files that end with 2 uppercase letters.

fixatd
  • 1,394
  • 1
  • 11
  • 19