3

Problem:

In my "D:/collection" folder, I have two types of files, that are "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,....

Objective:

I want to take only the "8-digit-number_type.dbf" files, and exclude the "character_type.dbf" files from my analysis. I don't care about the "character_type.dbf" files. Then, I'd like to split the 8 digits into two 4-digit values. So, a listing file(say,list.txt) with two columns should look like the following:

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

And these values should be stored as character type.

How can I implement that in Python? Thank you.

Sincerely, Bill TP

0

2 Answers 2

3
>>> 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
Sign up to request clarification or add additional context in comments.

3 Comments

You might as well give him os.listdir(path) if you're going to do it all for him :-)
I'll leave that up to the OP because it's easier to test it this way :D
Thanks, guys. One more question: printing does not make a text file that contains the printed values. I wonder how to write the values into a text file (say, mylistout.txt).
1

To use os.listdir(path) with jamylak's solution, simply put the import statement from os import listdir on the second line and change the paths declaration to paths = listdir('D:/')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.