Udacity课程编程基础:Python
自动打开打开网页
import webbrowser
webbrowser.open("http://v.youku.com/...")
重命名文件夹下的所有文件
import os
def rename_file():
# get files' names from a diretory
file_list = os.listdir(r"C:\Users\Administrator\Desktop\prank")
save_path = os.getcwd() # Return a string representing the current working directory
# print("Current Working Directory is "+save_path)
# Change the current working directory to path
os.chdir(r"C:\Users\Administrator\Desktop\prank")
# rename each file
for file_name in file_list:
map = file_name.maketrans('a','a','0123456789')
os.rename(file_name, file_name.translate(map))
rename_file()
或者可以使用re.sub来替换,本例中替换数字可以为:re.sub(r'[0-9]+', '', file_name)
注:当文件已存在时,无法创建该文件
画图
import turtle
window = turtle.Screen()
window.bgcolor("white")
abc = turtle.Turtle()
abc.shape("arrow")
abc.color("light blue",'light green')
abc.speed(6)
abc.pen(pensize=5)
abc.penup()
abc.setposition(-150, -150)
abc.pendown()
for k in range(3):
abc.forward(480)
abc.left(120)
for i in range(3):
abc.forward(240)
abc.left(120)
abc.color("light blue", 'light green')
abc.begin_fill()
for j in range(3):
abc.forward(120)
abc.left(120)
abc.end_fill()
window.exitonclick()
import turtle
def draw_square(some_turtle):
for m in range(36):
for n in range(4):
some_turtle.forward(100)
some_turtle.right(90)
some_turtle.right(10)
def draw_circle(some_turtle):
for l in range(3):
for m in range(7):
some_turtle.circle(m * 15)
some_turtle.left(120)
def draw_triangle(some_turtle):
for m in range(6):
some_turtle.left(60)
for n in range(3):
some_turtle.right(120)
some_turtle.forward(240)
def draw_art():
window = turtle.Screen()
window.bgcolor("green")
brad = turtle.Turtle()
brad.shape('turtle')
brad.color('black')
brad.speed(10)
draw_square(brad)
angie = turtle.Turtle()
angie.shape('arrow')
angie.color('blue')
angie.speed(10)
draw_circle(angie)
lulu = turtle.Turtle()
lulu.shape('arrow')
lulu.color('orange')
lulu.speed(8)
draw_triangle(lulu)
window.exitonclick()
draw_art()