Author Topic: [Python] CountMyFiles  (Read 1425 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
[Python] CountMyFiles
« on: June 07, 2011, 01:46:11 pm »
Well, I just started learning python, like a week ago or something. Cool language and very much like Delphi. For starters I re-wrote my Pascal CountMyFiles app, which can be found here: http://pastebin.com/ztm1aazY
And this is the python version (http://newage.ql.lt/projects/python/CountMyFiles.py):
Not pretty, but meh, it's a start :D gonna make it better sometime...

Code: [Select]
'''
Written on: 2011.06.07
Last update: 2011.06.12
Kulverstukas;
'''
import os
import string
import sys

dir = ''
ext = ''
WhatToSearch = ''
filename = []
filesize = []
arguments = []
TempStr = ''
TempInt = 0
FolderCount = 0
FileCount = 0

#=========Start of: AddChar function
def AddChar(Number,WhatChar):
    Chars = ''
    while Number != 0:
        Chars = Chars + WhatChar
        Number = Number - 1
    return Chars
#=========End of: AddChar function

#=========Start of: Header
print '\n'
print '      File counter. Simple crapware;'
print '      Parameters: --dir=DIRECTORY || --search=files/folders || --ext=EXTENSION - [Leave blank to search for all files]'
print '\n'
#=========End of: Header

#=========Start of: Read the arguments into a list
arguments = sys.argv[1:]
if len(arguments) < 3:
    print 'Not enough parameters nigger!'
    exit()
#=========End of: Read the arguments into a list

#=========Start of: loop to check the directory
for dir in arguments:
    if string.find(dir,'--dir=') != -1:
        dir = string.replace(dir, '--dir=', '')
        break
if (os.path.isdir(dir) == False):
    print 'The path does not exist. Fix0r pl0x'
    exit()
#=========End of: loop to check the directory

#=========Start of: what to search - files or folders
for WhatToSearch in arguments:
    if string.find(WhatToSearch,'--search=') != -1:
        WhatToSearch = string.replace(WhatToSearch, '--search=', '')
        break
#=========End of: what to search - files or folders
   
#=========Start of: file search+count and folder count
if WhatToSearch == 'files':
    for ext in arguments:
        if string.find(ext,'--ext=') != -1:
            ext = string.replace(ext, '--ext=', '')
            break
    if ext == '':  # if blank, find all the files
        for TempStr in os.listdir(dir):  # start finding all the files
            if (os.path.isdir(os.path.join(dir,TempStr)) == True): FolderCount = FolderCount + 1
            if (os.path.isfile(os.path.join(dir,TempStr)) == True):  # if a file is found
                FileCount = FileCount + 1
                filename.append(TempStr)  # get it's name
                filesize.append(os.path.getsize(os.path.join(dir,TempStr))) # get it's size
    else:  # if an extension was given
        for TempStr in os.listdir(dir):  # start finding all the files
            if (os.path.isdir(os.path.join(dir,TempStr)) == True): FolderCount = FolderCount + 1
            if (os.path.isfile(os.path.join(dir,TempStr)) == True) and (TempStr.endswith(ext) == True):  # if an extension was found in the file
                FileCount = FileCount + 1
                filename.append(TempStr)
                filesize.append(os.path.getsize(os.path.join(dir,TempStr)))
#=========End of: file search+count and folder count

#=========Start of: folder search+count and file count
if WhatToSearch == 'folders':
    for TempStr in os.listdir(dir):
        if (os.path.isfile(os.path.join(dir,TempStr)) == True): FileCount = FileCount + 1
        if (os.path.isdir(os.path.join(dir,TempStr)) == True):
            FolderCount = FolderCount + 1
            filename.append(TempStr)
            filesize.append(os.path.getsize(os.path.join(dir,TempStr)))
#=========Start of: folder search+count and file count 

#=========Start of: Output
print '\n'
print '  File name'+AddChar(59,'.'),'File size'
print AddChar(80,'=')
while TempInt < len(filename):  # print all the items in filename array
       
    if filesize[TempInt] > 1024:
        filesize[TempInt] = filesize[TempInt] / 1024
        TempStr = str(filesize[TempInt])+' Kilobytes'
    else: TempStr = str(filesize[TempInt]) +' Bytes'   
       
    print filename[TempInt]+AddChar(70-len(filename[TempInt]),'.'),TempStr
    TempInt = TempInt + 1
print AddChar(80,'=')
print 'Found files:',FileCount,';   Found folders:',FolderCount
#=========End of: Output

« Last Edit: June 12, 2011, 07:33:59 am by Kulverstukas »

Offline 10n1z3d

  • Serf
  • *
  • Posts: 42
  • Cookies: 8
    • View Profile
Re: [Python] CountMyFiles
« Reply #1 on: June 13, 2011, 05:36:03 pm »
You should try to keep your code width less than 80 characters. It looks better and it's a good practice. Most code editors can show a line on the right which will guide you. If you are using gedit, go to Edit->Preferences and check Display right margin, then for Right margin at column select 80.
Code: [Select]
python -c "print ''.join(chr(x) for x in [int(oct(39)) + 2, 24 * 2, 313 % 203, 0x31, (2 ** 7) - 6, int('051'), (3 << 6) - 92])"