Alphalearningschool
        

<<< Prev

Next >>>

Up
Python OS module

In this os module in python we can do many operation like rename, delete, remove etc..

Let see the Example for that, 

Ex.1
import os
os.rename("demo2.txt","demo.txt") #now check your file destination, name is change
os.remove("index.txt")

o/p-	
C:\ProgramData\Anaconda3\python.exe "D:/New folder/PycharmProjects/Loops/Set2.py"

 

Ex.2
import os
#Now creating a new directory
#os.mkdir("Temp")
#Change directory from current directory
#os.chdir("HP")
#Get current directory path
print(os.getcwd())
#Now remove the directory
os.rmdir("Temp")
#step by step you should remove the comment and see what output will come in consol.

O/p
D:\New folder\PycharmProjects\Loops 

 

Some file related function in Python:

1

file.close()

This command use for close the file.

2

File.fush()

It is use for flush the internal buffer.

3

File.fileno()

It will returns the integer file descriptor used by the underlying implementation to request I/O from the OS.

4

File.isatty()

It returns true if the file is connected to a (tty) device, otherwise returns false.

5

File.next()

It will return the next line each when file will be call.

6

File.read([size])

It will read a file for particular size.

7

File.readline([size])

It will read entire line from file.

8

File.readlines([sizehint])

Same as readline([size]) function. It reads the file until the EOF occurs using readline() function.

9

File.seek(offset[,from)

It will modify the file pointer.

10

File.tell()

It will return the current position of the file.

11

File.truncate([size])

It truncates the file size.

12

File.write(str)

This function use for write a file.

13

File.writelines(seq)

This will write sequence string to the file.

Python Module

Python module is nothing but such as python library. A module allow you to write a python code in logically and proper manner.

Let see with one simple example we can save the python file with .py extension.

def myFunc(name):
    print("Hello" , name)
    return
myFunc("Chintan")


O/P
Hello Chintan

 

 

Two main module in our python code:

  1. The import statement
  2. The from-import statement

Let understand both the module,

The Import statement: 

This statement uses for import all functionality of module in other module.

We can import multiple modules in single import statement.

Like import module1 , module2 , module3

Now understand with example

import os 
print("We can use inbuilt function of OS module")
#os.remove("demo.txt") #Remove the file from particular path
os.rename("demo.txt","index.txt") #For changing the name of current file

O/p
We can use inbuilt function of OS module

 In above example we can use inbuilt function or method of OS module to get the output.

 The from-import statement: Instead of importing all the functionality or method of module we can only import that thing whatever we need for this program.

Like from module import name

Let see the example for that,

Ex.
#Cal.py
def add(a,b):
    print("Addition of Two variable ",a+b)

def sub(a,b):
    print("Subtraction of two variable ",a-b)
def mul(a,b):
    print("Multiplication of two variable ",a*b)

 

#Main.py
from Cal import add

a=int(input("Enter Your number ="))
b=int(input("Enter your number ="))
(add(a,b))

O/p
Enter Your number =1
Enter your number =5
Addition of Two variable  6

 

Above the example we will not import the full functionality of Cal.py instead of whatever we need that much we import.

It always batter to go from-import statement instead of import because import statement make our code heavier.

Scope of variable

In python, variable are associated with two types of scopes. All the variable defined in a module contain global variable and inside the method or function variable we call local variable.

Local variable scope lies inside the method or function body we cannot access the local variable globally.

But if you declare the variable with same and different scope that time priority always given by local variable.

Let see the example for that,

Ex.
a=10
def hello(a):
    print(a)
print(input("Enter the Number :"))

O/p
Enter the Number : 15
 15