Professional Documents
Culture Documents
(203105467)
Mr. Suman
Ch - 5Shekhar , Assistant
Database and Professor
File Operations
● Syntax:
● Connection-Object= mysql.connector.connect(host = <host-
name> , user = <username> , passwd = <password> )
Database Connection
Import mysql.connector module
● Example:
Database Connection
Import mysql.connector module
● Here, we must notice that we can specify the database name in
the connect() method if we want to connect to a specific
database.
Database Connection
Database Connection
Create the cursor object
● Reading a File
● The .readline() method prints each line from our file, every time we
execute this function, until the end of the file is reached. This will
print the first two lines of our file, separated by a newline
character.
● The .readlines() method always gives a list of all the lines of our file
as list elements.
File operation
● Reading a File
● The .readline() method prints each line from our file, every time we
execute this function, until the end of the file is reached. This will
print the first two lines of our file, separated by a newline
character.
● The .readlines() method always gives a list of all the lines of our file
as list elements.
File operation
● Reading a File
● A major difference between the .read() method and the .readline()
method is, .read() method focuses on reading each character from
a file while the .readline() focuses on reading individual lines from a
file.
File operation
● Writing/Creating a File
● If we want to add content to our existing file, myfile.txt, first we
need to open it in the ‘w’ write mode and use the .write()
method on our file. The content to be written into the file needs
to be passed as a string. It’s a good practice to add a newline
character ‘n’ to add a line between sentences. This improves the
readability for the user and is also useful while using the
.readline() and .readlines() methods to distinguish between the
lines.
File operation
● Writing/Creating a File
● Here, on execution, the .write() method returns the number of
characters written into the file.
File operation
● Writing/Creating a File
● Another file mode used for writing to the file is the ‘a’ append
mode. This creates a new file if the specified file does not exist in
the current working directory. Unlike ‘w’ mode, it does not
remove the existing content from the file. Instead, it appends or
adds the new content at the end of the file. Writing in append
mode can be done using the .write() method.
● Example:
File operation
● Closing a File in Python
● At last, after opening and performing the reading, writing
operations, it is important to close the file. This is done using
.close() method. Let’s understand this with an example:
File operation
● Truncating a File
● Truncating a File is also possible in Python. By using the
.truncate() method, we can truncate the file up to the desired
length. Let’s understand this with an example:
File operation
● Renaming a File
● Renaming a file in Python can be done using the os Module and
needed to be imported to perform such tasks. The os module in
Python has a vast collection of methods to perform all the
essential file management tasks in Python itself.
File operation
● Deleting a File
● We can remove the files from a directory using Python’s versatile
os module. The os module has a method .remove() which
performs the task. The .remove() method takes a single
argument as string type which is our file name. Let’s understated
this with an example.
www.paruluniversity.ac.in