0% found this document useful (0 votes)
38 views4 pages

File Manipulation

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)
38 views4 pages

File Manipulation

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/ 4

ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

FILE MANIPULATIONS
File manipulation means change or access the content in the file.
 File Positions
 Renaming and Delete a File
 Directories in Python

1. File Positions
There are two methods to access the positions of the file.
1. tell()
2.seek()
i) tell() method
This method is used to tell the current position within a file. It starts from the beginning
of the file and this method followed by read() and write() method.
Syntax:

file_object.tell()

ii) seek() method


This method is used to change the current file position.
Syntax:
file_object.seek(offset, from)

 seek() method set the file’s current position at the offset.

GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING


ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

 The default argument of offset is 0. The offset represents the number of the bytes to be
moved.
The from argument represents three values.
0  represents the beginning of the file
1 represents the current position as reference
2 represents the end of the file

Program to use read(), tell(),seek() methods


sample.txt - Problem solving and python programming

f1=open("F:/Python/sample.txt","r")
str=f1.read(10)
print(“read string is : “,str)
position=f1.tell()
print(“current file position:”,position)
position=f1.seek(0,0)
str=f1.read(10)
print(“again read string is :”,str)
f1.close()

Output:

read string is: Problem So


current file position : 10
again read string is : Problem So

RENAMING AND DELETING A FILE


Two file processing operations are there, they are
 rename() method
 remove() method
i) rename() method
The rename() method takes two argument, the current filename and new
filename.

os.rename(current_filename, new_filename)
GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

Syntax:
Example

import os
os.rename(“test.txt”,”new.txt ”)

ii) remove() method


The remove() method is used to delete the file. The argument contains file name.
Syntax:

os.remove(filename)

Example:

import os
os. remove(“new.txt ”)

DIRECTORIES IN PYTHON
All files are contained within various directories. The os module has several methods to
create, remove and change directories.

S.No Name Syntax Description Example

1 mkdir() os.mkdir(“new_dir”) This method is used to os.mkdir(“test”)


create a directory

2 chdir() os.chdir(“new_dir”) This method is used to os.chdir(“new_dir”)


change a directory

3 getcwd() os.getcwd() This method is used to os.getcwd()


display current
directory

4 rmdir() rmdir(‘dir_name’) This method is used to os.rmdir(‘new_dir’)


remove a directory

GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING


ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

FORMAT OPERATOR
The argument of write has to be a string, so if we want to put other values in a file, we
have to convert them to strings. The easiest way to do that is with str:
f=open('stringsample.txt','w')
f.write(5) #TypeError
f.write(str(5))
An alternative is to use the format operator, %. The first operand is the format string,
which contains one or more format sequences, which specify how the second operand is
formatted. The result is a string.
Example:
var=8
print("The Value is : %d"%var)
Output:
The Value is : 8

GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING

You might also like