-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetFilesInFolderAndCreateCSV
More file actions
40 lines (28 loc) · 1.29 KB
/
GetFilesInFolderAndCreateCSV
File metadata and controls
40 lines (28 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# csv writerow method
from os import listdir
from os.path import isfile, join
import csv
# initialize the read and wite paths
readPath = 'readpath/folder'
writePath = 'writepath/folder'
# loops through the files in the read path and gets all FILES that ENDS WITH .fileExtension
filesInFolder = [f for f in listdir(readPath) if isfile(join(readPath, f)) and f.endswith('.fileExtension')]
# create sthe list.csv file in writepath and pushes in the files list
with open(writePath + '/list.csv', 'w', newline ='') as myfile:
wr = csv.writer(myfile, delimiter='\n')
wr.writerow(filesInFolder)
print('Done!')
#########################################################################################################
# pandas method
from os import listdir
from os.path import isfile, join
import pandas as pd
# initialize the read and wite paths
readPath = 'readpath/folder'
writePath = 'writepath/folder'
# loops through the files in the read path and gets all FILES that ENDS WITH .fileExtension
filesInFolder = [f for f in listdir(readPath) if isfile(join(readPath, f)) and f.endswith('.fileExtension')]
# Create a dataframe from the list and uses pandas to create the csv file
df = pd.DataFrame(filesInFolder, columns=['filesInFolder'])
df.to_csv(writePath + '/list.csv', index=False)
print('Done!')