How to get a list of file names in a directory in Python

Problem:

In my folder "D: / collection" I have two types of files: "8-digit-number_type.dbf" and "character_type.dbf" as follows: 00093282.dbf, 00345602.dbf, 69209340. dbf, 69806980.dbf, 92406482.dbf, 93609360.dbf, ..., aaa.dbf, bbb.dbf, ....

Purpose:

I want to take only the files "8-digit-number_type.dbf" and exclude the files "character_type.dbf" from my analysis. I do not need character_type.dbf files. Then I would like to break 8 digits into two 4-digit values. So, a listing file (say list.txt) with two columns should look like this:

0009 3282
0034 5602
6920 9340
6980 6980
9240 6482
9360 9360
....

And these values ​​should be stored as a character type.

How can I implement this in Python? Thank.

Regards, Bill TP

+3
source
2
>>> import os
>>> paths = ['00093282.dbf', '00345602.dbf', '69209340.dbf', '69806980.dbf', '92406482.dbf', '93609360.dbf','aaa.dbf', 'bbb.dbf']
>>> for path in paths:
        file_name,ext = os.path.splitext(path)
        if file_name.isdigit():
            print '{0} {1}'.format(file_name[:4],file_name[4:])


0009 3282
0034 5602
6920 9340
6980 6980
9240 6482
9360 9360
+3

os.listdir(path) jamylak, import from os import listdir paths = listdir('D:/')

+1

All Articles