Day7

Pramod Ray
2 min readSep 11, 2019

--

Os module : The OS module in python provide function for interacting with the operating system. The module provide a portable way of using operating system dependent functionality.

Os.getcwd(): It returns the current working directory.

Os.chdir(): to change the current working directory to path

os.chdir(foldername): step to step forward directionos.chdir('.'): backword direction

os.listdir() :- it returns a list containg the name of entires in the directory given by path.

os.remove() : — remove the file .

os.rename(oldName,newName): It is used for rename file name.

os.path.exists(path) : It returns True if path refers to an existing path else False.

os.path.relpath(path,[start]): returns a relative filepath to path either from current directory or not.

os.path.abs(path): Retuens the normalized absolutized version of the pathname path.

os.path.isabs(path): Returns True the path is absolute pathname.

os.path.isfile(path) : It retuns True if path is an existing regular file.

import os
os.path.isfile('/home/pramodray/hello.txt')

os.path.isdir(path) : It returns True if path is an existing directory.

import os
os.path.isdir('/home/pramodray/')

File Handling

File is named location on the system storage which records data for later access. In file we can read write by using with different mode

Mode in File Handling : In file Handling there are 12 modes but in general 4 modes are is using,

mode    description
r Read only
w write only
r+ Read and write both
a appending the file

open() : To open a file in Python use its built open() function.

Syntax: file_object=open('filename','mode')

close(): It is used mainly for exiting the file

Syntax : file_object.close()

write() : To write any string to open file.

Syntax: file_object.write('content')

read() :To reads a string from open file.

file_object.read(number)

tell(): The tell() method tells you the current position within the file.

file_object.tell()

seek() : seek(offset[,from]) method change the current file position.

fileObject

--

--

Pramod Ray
Pramod Ray

No responses yet