What I would like to do is to check the size of an archive directory in each of our users folders accessible via a Windows machine. I have a txt file with all of the users names (1 per line) and am trying to use for loops and potentially readline to iterate over the file and us os.path.walk to determine the size. I have confirmed that my syntax for 1 directory work, but when I try to iterate, it does not work. Also, when printing the value of the path for each iteration I get the first part on one line and then the last part of the path on another. Please help with the syntax. Below is where I have left off:

# This script will determine the size if the gwarchive directories in everyone's home folder

import os
import fileinput

folder_size = 0

for username in fileinput.input("L:\\dirl.txt"):
    folder = ("L:\\" + (username) + "\gwarchiv")
    print folder
    for (path, dirs, files) in os.walk(folder):
        for name in files:
            filename = os.path.join(path, name)
            folder_size += os.path.getsize(filename)
        print "Archive = %0.1f MB" % (folder_size/(1024*1024.0))
Re: Need help with Python syntax for loop 80 80

Try this ...

# This script will determine the size if the gwarchive directories 
# in everyone's home folder

import os

folder_size = 0

for username in open("L:\\dirl.txt"):
    folder = "L:\\" + username + "\\gwarchiv"
    print folder
    for (path, dirs, files) in os.walk(folder):
        for name in files:
            filename = os.path.join(path, name)
            folder_size += os.path.getsize(filename)
        print "Archive = %0.1f MB" % (folder_size/(1024*1024.0))
Re: Need help with Python syntax for loop 80 80

Yeah, I tried that as well with the "\\" before gwarchiv and I still get this during the print:

L:\zenzal
\gwarchiv
L:\ZhuM
\gwarchiv
L:\ZielJL
\gwarchiv
L:\ZimmBT
\gwarchiv
L:\zimmga
\gwarchiv
L:\ZimmSA
\gwarchiv
L:\zindas
\gwarchiv
L:\ZinkDA
\gwarchiv
L:\ZinkWP
\gwarchiv
L:\zinncs
\gwarchiv
Re: Need help with Python syntax for loop 80 80

Vegaseat forgot a little point

# This script will determine the size if the gwarchive directories 
# in everyone's home folder

import os

folder_size = 0

for username in open("L:\\dirl.txt"):
    folder = "L:\\" + username.strip('\n') + "\\gwarchiv" # <== remove the ending "\n"
    print folder
    for (path, dirs, files) in os.walk(folder):
        for name in files:
            filename = os.path.join(path, name)
            folder_size += os.path.getsize(filename)
        print "Archive = %0.1f MB" % (folder_size/(1024*1024.0))
commented: thanks, I didn't test my code +15
Re: Need help with Python syntax for loop 80 80

Changing username to username.strip('\n') was the solution. Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.