Skip to main content

How do you handle file and directory operations?

Mid Python
Quick Answer pathlib.Path is the modern way: Path("dir") / "file.txt" for path joining. path.exists(), path.is_file(), path.is_dir(). path.mkdir(parents=True, exist_ok=True). path.glob("*.txt") for file search. shutil.copy(), shutil.move(), shutil.rmtree() for file operations. os.walk() or Path.rglob() for directory traversal. Prefer pathlib over os.path for cleaner code.

Answer

Use os and pathlib for creating, deleting, and navigating files/directories.
Use shutil for copy/move/archive operations.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice