I am using python 2-7 to get file path and save in a CSV(Comma-separated values) file. It contains the path and all file in folder. With file in different folder, it is separated by comma and then the number of its folder without character. For example, I have image 1.pgm that stores in folder s14. Then the CSV format looks like
at/s14/1.pgm;14
In which, at/s14/1.pgm is full path to file 1.pgm and 14 is number (called label) that get from its folder s14. Could you help me implement it by python. I tried with that code but it does not work for my task
#!/usr/bin/env python
import sys
import os.path
# |-- s14
# | |-- 1.pgm
# | |-- ...
# | |-- 10.pgm
# |-- s20
# | |-- 1.pgm
# | |-- ...
# | |-- 10.pgm
# ...
# |-- s40
# | |-- 1.pgm
# | |-- ...
# | |-- 10.pgm
#
if __name__ == "__main__":
if len(sys.argv) != 2:
print "usage: create_csv <base_path>"
sys.exit(1)
BASE_PATH=sys.argv[1]
SEPARATOR=";"
label = 0
for dirname, dirnames, filenames in os.walk(BASE_PATH):
for subdirname in dirnames:
subject_path = os.path.join(dirname, subdirname)
for filename in os.listdir(subject_path):
abs_path = "%s/%s" % (subject_path, filename)
print "%s%s%d" % (abs_path, SEPARATOR, label)
label = label + 1
The expected output is
at/s14/1.pgm;14
at/s14/2.pgm;14
....
at/s14/10.pgm;14
at/s20/1.pgm;20
at/s20/2.pgm;20
....
at/s20/10.pgm;20
....
at/s40/1.pgm;40
at/s40/2.pgm;40
....
at/s40/10.pgm;40
In which at is root folder path that contains subfolder s14,s20... In window, it look like "E:\at"