Feb 28 2013

Python Commands and Functions

Category: PythonFatih Acar @ 16:50

Take value from user.

input(“Enter a number : “)
raw_input(“Enter a character : “)

Counters

range(100) # write from 0 to 100
range(100,200) # write from 100 to 200
range(100,200,2) # write from 100 to 200 as 2 by 2
len(“Fatih”) # character count result 5

File and Directory Operations

listdir(“/home”/) # list all file or directory in /home/ directory

a=os.listdir(“/home/”)
for files in a:
print files

getcwd() # show current directory
chdir(“/var/lib”) # change directory
mkdir(“test”) # create directory
makedirs(“/test/test1/test2”) # create nested directories
rmdir(“test”) # remove directory
removedirs(“/test/test1/test2”) # remove directories
open(“test.txt”,”w”) # create file and delete all exist values and open with write mode
open(“test.txt”,”a”) # open exist file with append mode
open(“test.txt”,”r”) # open exist file with read mode

file_a.open(“test.txt”,”w”)
file_a.write(“Hello World with Python !”)
file_a.close()

file_a.open(“test.txt”,”r”)
file_a.read()
file_a.readlines() # read line by line

remove(“test.txt”) # delete file
rename(“test.txt”,”test1234.txt”) # rename file
str(a) # convert to string
int(a) # convert to integer

31,327 total views, 5 views today

Tags: Linux Administration, Programming, Python, System Administration