-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory reader.py
More file actions
41 lines (31 loc) · 1.57 KB
/
directory reader.py
File metadata and controls
41 lines (31 loc) · 1.57 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
41
# Script file to read all the files of the specified directory and
# store it in a text file called "file content.txt" in the current
# working directory
import os
# for functions os.listdir(), os.path.join() and os.path.getsize() functions
fout = open('file contents.txt','w')
# opens the text file or creates if not created of the name "file contents.txt"
# in the current working directory which can be accessed by fout object in
# writing mode
line1 = "File name Size \n"
# for giving the heading of the columns (File name and Size)
fout.write(line1)
# writes the above line in the file through its file object (fout)
files = os.listdir('H:\\backup\\docx') # location of the directory
# storing the names of all the files in the files variables in the form of list
# using the os.listdir() function
for file in files:
# for loop for reading the list's each item and finding each file's size in
# the directory
location = os.path.join('H:\\backup\\docx\\',file)
# joins both the path and the file name together
# stores the complete location in the location variable
line = file +" "+ str(os.path.getsize(location)) + "\n"
# stores the filename along with its size in the variable line
# os.path.getsize() returns a long value n must be converted
# to string before writing it into the file
fout.write(line)
# the line variables content are written inside the file object (fout) and
# hence to the the text file ("file contents.txt")
fout.close()
# closes the file object and the file which is opened through the file object