3.ɨÃ赱ǰĿ¼ºÍËùÓÐ×ÓĿ¼²¢ÏÔʾ´óС¡£
- import os
- import sys
- try:
- directory = sys.argv[1]
- except IndexError:
- sys.exit("Must provide an argument.")
- dir_size = 0
- fsizedicr = {'Bytes': 1,
- 'Kilobytes': float(1) / 1024,
- 'Megabytes': float(1) / (1024 * 1024),
- 'Gigabytes': float(1) / (1024 * 1024 * 1024)}
- for (path, dirs, files) in os.walk(directory):
- for file in files:
- filename = os.path.join(path, file)
- dir_size += os.path.getsize(filename)
- fsizeList = [str(round(fsizedicr[key] * dir_size, 2)) + " " + key for key in fsizedicr]
- if dir_size == 0: print ("File Empty")
- else:
- for units in sorted(fsizeList)[::-1]:
- print ("Folder Size: " + units)
4.½«Ô´Ä¿Â¼240ÌìÒÔÉϵÄËùÓÐÎļþÒƶ¯µ½Ä¿±êĿ¼¡£
- import shutil
- import sys
- import time
- import os
- import argparse
- usage = 'python move_files_over_x_days.py -src [SRC] -dst [DST] -days [DAYS]'
- description = 'Move files from src to dst if they are older than a certain number of days. Default is 240 days'
- args_parser = argparse.ArgumentParser(usage=usage, description=description)
- args_parser.add_argument('-src', '--src', type=str, nargs='?', default='.', help='(OPTIONAL) Directory where files will be moved from. Defaults to current directory')
- args_parser.add_argument('-dst', '--dst', type=str, nargs='?', required=True, help='(REQUIRED) Directory where files will be moved to.')
- args_parser.add_argument('-days', '--days', type=int, nargs='?', default=240, help='(OPTIONAL) Days value specifies the minimum age of files to be moved. Default is 240.')
- args = args_parser.parse_args()
- if args.days < 0:
- args.days = 0
- src = args.src # ÉèÖÃԴĿ¼
- dst = args.dst # ÉèÖÃÄ¿±êĿ¼
- days = args.days # ÉèÖÃÌìÊý
- now = time.time() # »ñµÃµ±Ç°Ê±¼ä
- if not os.path.exists(dst):
- os.mkdir(dst)
- for f in os.listdir(src): # ±éÀúԴĿ¼ËùÓÐÎļþ
- if os.stat(f).st_mtime < now - days * 86400: # ÅжÏÊÇ·ñ³¬¹ý240Ìì
- if os.path.isfile(f): # ¼ì²éÊÇ·ñÊÇÎļþ
- shutil.move(f, dst) # Òƶ¯Îļþ
5.ɨÃè½Å±¾Ä¿Â¼£¬²¢¸ø³ö²»Í¬ÀàÐͽű¾µÄ¼ÆÊý¡£
- import os
- import shutil
- from time import strftime
- logsdir="c:\logs\puttylogs"
- zipdir="c:\logs\puttylogs\zipped_logs"
- zip_program="zip.exe"
- for files in os.listdir(logsdir):
- if files.endswith(".log"):
- files1=files+"."+strftime("%Y-%m-%d")+".zip"
- os.chdir(logsdir)
- os.system(zip_program + " " + files1 +" "+ files)
- shutil.move(files1, zipdir)
- os.remove(files)
£¨±à¼£ºASPÕ¾³¤Íø£©
|