You are on page 1of 1

PYTHON FILES AND OS:

To check the file present or not or it is file or directory:


import os
file=/////asd.txt
If os.path.isfile(file) == True:
Print there
else:
Print not
********************************
Get the current director
os.getcwd()
**************************************
To check the file present in the current directory or not :
Files = os.listdir((os.getcwd))
file =abc.txt
If file in files:
Print if it there
else:
Print it is not there
**************************************
To write the content of the file to the current directory if the file os not fou
nd then
Import sys,os
a = [Enter the content in list format each element in one line ] , b = then join
by \n.join(a) use b to write data in file
a= [control file +sys.argv[0] +this is the cscript,
out= + os.getcwd() ,
Name = /p/sad/sd/d/asd/sd/d/sd/s/d/
Text = /asdasd/asd/asd/asd/ad/d/ds/d/s/s/d.txt
]
b= /n.join(a)
Now use b to print or write in any other file
********************************************************************************
*******
os.getcwd() ----> get the current working directory
os.path.basename(__file__) ---> get the file name executing
os.path.abspath(__file__) ----> get the realpath of the file
os.path.dirname(__file__)-----> get the directory path for the file
os.getlogin() get the username
Os.environ ---> get the environment variables
os.eniron[HOME] get he home userx
********************************************************************************
*******************

You might also like