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/ztm1aazYAnd this is the python version (
http://newage.ql.lt/projects/python/CountMyFiles.py):
Not pretty, but meh, it's a start
gonna make it better sometime...
'''
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