Deepansi
Deepansi
Presentation
The concept of
renaming, deleting a file
in python & dictionaries
in python
Introduction
Python is a popular and versatile programming language that is used in a
wide variety of applications from web development to scientific
computing. One of its most useful feature is the ability to rename file,
deleting files and dictionaries using built in functions. let us understand
these concepts in detail
Renaming file
In simple terms the rename() function allows you to change the name
of a file or directory in your python program. This can be
particularly useful in situations where you need to organise or
manage your files programmatically. For example you may want to
rename a file based on its contents or you may need to rename a
directory to better reflects its contents. The rename() function is a
part of the built in os module in python which provides a range of
functions for interacting with the operating system
Renaming single python file
import os
SYNTAX:
import os
os.remove(“demofile.txt”)
How to delete a
file
import os
if
os.path.exists(“demofile.txt”):
os.remove(“demofile.txt”)
else:
print(“the file does not exist”)
Dictionaries
Python dictionaries are essential for efficient data mapping in programming. To deepen
your understanding of dictionaries and explore advanced techniques in data handling.
Dictionaries in python is a data structure used to store values in key: value format. This
makes it different from list, tuples, and arrays as in a dictionary each key has an
associated value.
In the above dictionary Dict the keys Name and Age are the strings which comes under the
category of an immutable object
Example Program
Employee={”Name”:”Johnny”,”Age”:32,”salary”:26000,”Company”:”^TCS”}
print(type(Employee))
print(“printing Employee data....”)
print(Employee)
OUTPUT:
<class’dict’>
printing Employee data....
{’Name’:’Johnny’,’Age’:32,’salary’:26000,’Company’:TCS}
Advantages of dictionaries
Flexibility: Dictionaries stores data as key value pairs, which can be more flexible than
lists.
Versatility: Dictionaries can store data types, including numbers, texts, tuples, and
other dictionaries.
Efficient data access: Dictionaries allow for quick searches and access to information.
Handling missing keys: The defaultdict type can automatically create a key and
generate a default value if a missing key is accessed or modified.
Iterating through dictionaries: The built in reversed() function can be used to iterate
through dictionaries in reverse order.
Thank You
PRESENTED BY: SANIA
SHARMA
BCA 3RD SEMESTER
2311990