Python Directory and Files Management

1) List Current Directory

Write a program that prints all files and folders in the current directory.


2) Create a New Directory

Ask the user for a folder name and create it using os.makedirs().


3) Remove an Empty Directory

Ask for a directory name and remove it if it exists.


4) Create a New File

Ask the user for a file name and create that empty file using Python.


5) Write to a File

Ask for a file name and text, then write the text to the file.


6) Read a File

Ask for a file name and print its entire contents.


7) Append to a File

Ask for a file name and text, then append the text to the file.


8) Check If File Exists

Ask the user for a file name and print whether it exists or not.


9) Rename a File

Ask for an old name and new name, then rename the file.


10) Delete a File

Ask for a file name and delete the file if it exists.


11) Display Only Files

List and print only files (not directories) in the current folder.


12) Display Only Directories

List and print only directories in the current folder.


13) Create Multiple Files

Ask for a number n, then create n empty files with names: file1.txt, file2.txt, ….


14) Count Files

Count and print the total number of files in the current directory.


15) Path Join

Ask the user for a folder and file name, then print the full path using os.path.join().


16) File Size

Ask for a file name and print its size in bytes using os.path.getsize().


17) Copy a File

Ask for an existing file and a destination file name and copy it using shutil.copy().


18) Move a File

Ask for file name and destination directory, and move the file there using shutil.move().


19) Clear File Contents

Ask for a file name and delete all contents (without deleting the file itself).


20) Count Lines in a File

Ask the user for a file name and count how many lines it contains.


📌 Key Functions to Use

Task
Function / Module

List dir

os.listdir()

Create dir

os.makedirs()

Remove dir

os.rmdir()

Create file

open(file, 'w')

Read file

open(file, 'r')

Append file

open(file, 'a')

Exists check

os.path.exists()

Rename

os.rename()

Delete file

os.remove()

File size

os.path.getsize()

Copy

shutil.copy()

Move

shutil.move()


🧠 Example Starter Snippets

List directory contents

Create a folder

Write to a file


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated