加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python入门脚本的简单示例

发布时间:2020-12-17 07:10:09 所属栏目:Python 来源:网络整理
导读:感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。 python代码如下: #!/usr/bin/python#Filename: 1.pyi=5print ii=i+1print is=welcome to my home!nI will intorduce my family to you!print s,ilength=6width=5area=width * lengthprint Th
感兴趣的小伙伴,下面一起跟随编程之家 52php.cn的小编来看看吧。
python代码如下:

#!/usr/bin/python
#Filename: 1.py
i=5
print i
 
i=i+1
print i
 
s='welcome to my home!nI will intorduce my family to you!'
print s,i
 
length=6
width=5
area=width * length
print 'The Area is',area
print 'The perimeter is',2 * (length + width)
 
for a in range(2,9):
    print a
else:
    print 'The loop is over!'



#!/usr/bin/python
#Filename: 3.py
 
x=raw_input('Enter the value of x: ')
def printMax(x,y)
    if a>b:
        print x,'is the maximum'
    else:
        print y,'is the manimum'
printMax(x,y)



#!/usr/bin/python
# Filename: 2.py
while True:
    b = raw_input('Enter a sentense or words(exit for exit): ')
    if b == 'quit' or b == 'exit':
        break
    elif len(b) < 3:
        print 'your input is too short.The length of your input must over3. Please type again!'
        continue
    print 'Ok! The length of your input is over 3,your input is',b
print 'Done!'
&#91;/code&#93;
 
&#91;code&#93;
database=&#91;
&#91;"zhang3","0111"&#93;,&#91;"li4","0112"&#93;,&#91;"wang5","0113"&#93;
&#93;
username=raw_input("what's your user name?")
id=raw_input("what's your id?")
if &#91;username,id&#93; in database: print "access granted"
&#91;/code&#93;
 
&#91;code&#93;
def bj(x):
    if x<0:
         print "the number is too small"
    elif x>100:
         print "the number is too big"
    else: print "the number is right!"
 
x=input("please input a number:")
bj(x)


#读取文件的脚本
import os
if os.getcwd()!='/root':
    os.chdir('/root')
 
try:
    with open('hi.txt','r') as f:
        print(f.readline(),end='')
    f.close()
except IOError as err:    #此写法仅适用于python3
    print('File Error:' + str(err))

    
    
#!/usr/bin/python
import os
path1=os.getcwd()
path2=os.path.abspath('/usr/local/5.txt')
print('使用os.getcwd()获取的内容为' + path1)
print('使用os.path.abspath()获取的内容为' + path2)



#统计文件夹大小的脚本
import os  
from os.path import join,getsize  
   
def getdirsize(dir):  
   size = 0  
   for root,dirs,files in os.walk(dir):  
      size += sum([getsize(join(root,name)) for name in files])  
   return size  
   
if __name__ == '__main__':
   filesize = getdirsize('/tmp')  
   print ('There are ' + str(filesize/1024/1024) + 'Mbytes in /tmp')
   
   
#文件写入的简单脚本
import zipfile
f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED)
f.write('1.py')
f.write('/root/install.log')
f.close()


#压缩文件夹的脚本
#!/usr/bin/python
import zipfile
import os
f = zipfile.ZipFile('archive.zip',zipfile.ZIP_DEFLATED)
startdir = "/root/mydir"
for dirpath,dirnames,filenames in os.walk(startdir):
    for filename in filenames:
        f.write(os.path.join(dirpath,filename))
f.close()


#调用shell命令的脚本
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import commands
a=commands.getstatusoutput('pwd')
b=commands.getstatusoutput('pwdaa')
print('正确命令的返回状态是: ' + str(a[0]))
print('正确命令的返回结果是: ' + a[1])
print('错误命令的返回状态是: ' + str(b[0]))
print('错误命令的返回结果是: ' + b[1])
c=os.popen('pwd')
print c.read().rstrip()
# 来自52php.cn 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读