How do I create a directory, and any missing parent directories?

On Python ≥ 3.5, use pathlib.Path.mkdir: from pathlib import Path Path(“/my/directory”).mkdir(parents=True, exist_ok=True) For older versions of Python, I see two answers with good qualities, each with a small flaw, so I will give my take on it: Try os.path.exists, and consider os.makedirs for the creation. import os if not os.path.exists(directory): os.makedirs(directory) As noted in comments … Read more

very quickly getting total size of folder

You are at a disadvantage. Windows Explorer almost certainly uses FindFirstFile/FindNextFile to both traverse the directory structure and collect size information (through lpFindFileData) in one pass, making what is essentially a single system call per file. Python is unfortunately not your friend in this case. Thus, os.walk first calls os.listdir (which internally calls FindFirstFile/FindNextFile) any … Read more

How to copy a file along with directory structure/path using python? [duplicate]

To create all intermediate-level destination directories you could use os.makedirs() before copying: import os import shutil srcfile=”a/long/long/path/to/file.py” dstroot=”/home/myhome/new_folder” assert not os.path.isabs(srcfile) dstdir = os.path.join(dstroot, os.path.dirname(srcfile)) os.makedirs(dstdir) # create all directories, raise an error if it already exists shutil.copy(srcfile, dstdir)

Find all files in a folder

A lot of these answers won’t actually work, having tried them myself. Give this a go: string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); DirectoryInfo d = new DirectoryInfo(filepath); foreach (var file in d.GetFiles(“*.txt”)) { Directory.Move(file.FullName, filepath + “\\TextFiles\\” + file.Name); } It will move all .txt files on the desktop to the folder TextFiles.