• Coding

    Python Learning: Automate Boring Stuff with Python | Chapter 8 : My Solution to Practice Project: Deleting Unneeded Files

    Personally, one of the simplest projects yet This code (when properly indented) helps to list out all files with a size greater than the given limit.   [sourcecode language=”python” wraplines=”false” collapse=”false”] #Usage python big.py limit in MB <source> import sys, os if len(sys.argv) >= 2: limit = sys.argv[1] source = sys.argv[2] if len(sys.argv) == 3 else os.getcwd() source = os.path.abspath(source) if not os.path.exists(source): print("Provide valid source folder") else: for folder, subfolders, files in os.walk(source): for file in files: filepath = os.path.join(folder, file) MB = os.path.getsize(filepath) / 1000000 if MB > int(limit): print(filepath) [/sourcecode]

  • Coding

    Python Learning: Automate Boring Stuff with Python | Chapter 8 : My Solution to Practice Project: Selective Copy

    As usual… super simple automation.. I know it is quite trivial, but the fact that with a few lines of code I could re-organize my files based on their extensions makes coding really fun even outside of work.      [sourcecode language=”python” wraplines=”false” collapse=”false”] #selective copy #walks through a folder tree and searches for files with a certain file extension. #copies file matches into a new folder #Usage: python scopy.py <source> <destination> <extension> import sys, os, shutil if len( sys.argv ) == 4: #TODO: get source, destination and extension source = os.path.abspath(sys.argv[1]) destination = os.path.abspath(sys.argv[2]) extension = sys.argv[3] #TODO: validate source and destination if not os.path.exists(source): print("Invalid source path") if…

error: Content is protected !!