0% found this document useful (0 votes)
4 views

Deepansi

Uploaded by

mohammadwasif785
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Deepansi

Uploaded by

mohammadwasif785
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Project

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

#specify the file path and current file name


old_file_path=”/path/to/old_file.txt”

#specify the new file name


new_file_name=”new-file.txt”

#get the directory path of the old file


dir_path=os.path.dirname(old_file_path)

#construct the new file path


new_file_path=os.path.join(dir_path, ne_file_name)

#use the rename() method to rename the file


os.rename(old_file_path, new_file_path)
Deleting file
File management is a crucial aspect of code handling. Part of this skill set is knowing how to delete
a file. File deletion may be part of routine, periodic maintainence to clear hard drive space and
reduce bloat. But more often file deletion may be an integral part of a coding project to prevent
unnecessary clutter or confusion. Your code may generate temporary files for caching or logging
that are only useful for a short period and then need to be removed. You may generate duplicate
files, either intentionally or through testing, that need to be cleaned up.

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.

THE DATA IS STORED AS KEY-VALUE PAIRS USING A PYTHON


DICTIONARY:
The data structure is mutable
The components of dictionary were made using keys and values.
Keys must only have one components.
Values can be of any type, including integer, list, and tuple.
Creating a dictionary
Curly braces are the simplest way to generate a python dictionary, althrough there are other
approaches as well. With many key value pairs surrounded in curly brackets and a colon
separating each keys from its values, the dictionary can be built.
SYNTAX:
Dict={”Name”:”Gayle”,”Age”:25}

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

You might also like