Q1. Explain how to delete a file in Python?
In Python, file deletion is performed using either the modern **`pathlib`** module (recommended) or the traditional **`os`** module: ### 1. Using `pathlib` (Modern & Idio...
Technical Question Bank · Peer-Reviewed
Search and filter frequently asked interview questions across technical, programming, and behavioral domains. Tailor your interview preparation by difficulty for freshers or experienced developers (11 available).
In Python, file deletion is performed using either the modern **`pathlib`** module (recommended) or the traditional **`os`** module: ### 1. Using `pathlib` (Modern & Idio...
try: withopen('filename','r')asf: printf.read() exceptIOError: print"Nosuchfileexists" 75 How do we share global variables across modules in Python? We can create a confi...
Following is one possible solution there can be other similar ones: ```python import os ``` for dirname,dirnames,filenames in os.walk('.'): #printpathtoallsubdirectories...
withopen("filename.txt","r")asf1: printlen(f1.readline().rstrip()) rstrip() is an inbuilt function which strips the string from the right end of spaces or tabs (whitespac...
To delete a file in Python, you need to import the OS Module. After that, you need to use the os.remove() function. Example: 1 2 ```python import os ``` os.remove("xyz.tx...
While both files hold bytecode, .pyc is the compiled version of a Python file. It has platform-independent bytecode. Hence, we can execute it on any platform that support...
Let's create a text file on our Desktop and call it tabs.txt. To open it to be able to write to it, use the following line of code- ```python >>> file=open('tabs.txt','w'...
We have the following modes- read-only – 'r' write-only – 'w' read-write – 'rw' append – 'a' We can open a text file with the option 't'. So to open a text file to read i...
Python provides rich standard library modules for interacting with files, paths, and storage systems: 1. **`pathlib`** (Modern Standard): Object-oriented filesystem paths...
The most robust and pythonic way to read and print file contents is using the **`with` statement** (context manager), which guarantees the file descriptor is safely close...
HireXTech uses essential storage to remember your study preferences and third-party advertising cookies via Google AdSense in compliance with GDPR. You can choose to enable essential cookies only or accept all cookies. Read our Privacy Policy.