≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > Python

python入门脚本的简单示例

人气:604 时间:2018-10-08

这篇文章主要为大家详细介绍了python入门脚本的简单示例,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
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','w',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()
# 来自www.q1010.com 

本文来自:http://www.q1010.com/181/2136-0.html

注:关于python入门脚本的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:脚本

您可能感兴趣的文章

  • SQL Server 使用脚本或procedure查找dead lock及kill的简单示例
  • SQL Server 简单备份脚本入门实例
  • php 命令行(cli)执行PHP脚本文件的相对路径的问题解决方法
  • php date_default_timezone_set设定脚本日期函数的默认时区示例
  • php限制脚本执行时间功能实例
  • php脚本程序暂停执行函数的简单示例
  • PHP 检测codeigniter脚本消耗内存情况的简单示例
  • PHP codeigniter显示所有脚本执行时间的简单示例
  • jquery 三种动态加载JS脚本的实现方法
  • php 通过记录开始时间、结束时间计算脚本运行时间的简单示例
上一篇:python基础知识3:字典解析
下一篇:python的FTP模块的简单示例
热门文章
  • Python 处理Cookie的菜鸟教程(一)Cookie库
  • python之pandas取dataframe特定行列的简单示例
  • Python解决json.dumps错误::‘utf8’ codec can‘t decode byte
  • Python通过pythony连接Hive执行Hql的脚本
  • Python 三种方法删除列表中重复元素的简单示例
  • python爬虫代码示例
  • Python 中英文标点转换示例
  • Python 不得不知的开源项目解析
  • Python urlencode编码和url拼接实现方法
  • python按中文拆分中英文混合字符串的简单示例
  • 最新文章
    • Python利用numpy三层神经网络的简单示例
    • pygame可视化幸运大转盘的简单示例
    • Python爬虫之爬取二手房信息的简单示例
    • Python之time库的简单示例
    • OpenCV灰度、高斯模糊、边缘检测的简单示例
    • Python安装Bs4及使用的简单示例
    • django自定义manage.py管理命令的简单示例
    • Python之matplotlib 向任意位置添加一个子图(axes)的简单示例
    • Python图像标签标注软件labelme分析的简单示例
    • python调用摄像头并拍照发邮箱的简单示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。