subprocess调用第三方应用_datetime日期_time时间

在时间模块的time()的帮助下我们做了个秒表程序

时间模块的sleep()和日期模块datetime()共同配合使得我们可以在特定日期和时间启动程序的功能代码

在日期模块中我们接触了表达某一时刻和某一段时间的数据对象,两者加减使得计算某一日期前后1000天易如反掌。以前计算这个要考虑每月多少天是不是闰年,烦死人了!

通过strftime()可以把不好认的日期对象以我们制定的格式转换成日期字串。一个%w格式符就把这个日期是星期几算出来把我惊艳到了,以前算这个算得我很晕

以我们目前的水平写不出啥高大上的程序,但不要紧,现在我们有了秘密武器:

......那就是在我们的代码中用subprocess.run()启动外部程序。

有了它,不了解内情的人会以为我们水平很高,其实呵呵,我们只是把成熟的完善的外部程序用选择循环按自己的意思编排了一下而已。

比如我们就写很短的一段代码,倒数5、4、3、2、1,然后music音乐响起!

虽然怎么编写播放音乐的代码我不知道,但我知道怎么在代码中调用外部程序播放音乐呀

最后介绍一下unix设计哲学,就是unix主张设计一些短小的功能单一可以互相调用的小程序。因为小程序易于理解,而且通过互相调用,也可以像搭积木似的搭出功能强大的应用。

这个我原则我喜欢,因为写出复杂滴功能强大滴程序要学好久,

然后还不一定写得出来。
{
仔细琢磨琢磨,其实我们的手机上的app们早就这么干了。

时间模块:
 

日期模块:
 


调用外部程序:
 

import time

# print(time.time())

# time.time(): to measure how long a piece of code takes to run

# Calculate the product of the first 100000 numbers
def calcProd():
	product = 1
	for i in range(1, 100000):
		product = product * i
	return product
#
startTime = time.time()
prod = calcProd()
endTime = time.time()
print('The result is {} digits long'.format(len(str(prod))))
print('Take {} seconds to calculate'.format(endTime - startTime))

# time.sleep()
# for i in range(3):
# 	print('Tick')
# 	time.sleep(1)
# 	print('Tick')
# 	time.sleep(1)


# round()
# now = time.time()
# print(now)
# print(round(now,2))
# print(round(now,4))
# print(round(now))



# stopwatch.py - A simple stopwatch program.

import time

# Display instruction
print('Press Enter to begin. Afterwards, press ENTER to "click" the stopwatch. '
      'Press Ctrl-C(Windows) Command-F2(Mac) to quit.')

input() # press Enter to begin
print('Started.')
startTime = time.time() # get the first lap's start time
lastTime = startTime
lapNum = 1

# Start tracking the lap times
try:
    while True:
        input()
        lapTime = round(time.time() - lastTime, 2)
        totalTime = round(time.time() - startTime, 2)
        print('Lap #{}: {} ({})'.format(lapNum, totalTime, lapTime), end='')
        lapNum += 1
        lastTime = time.time() # reset the last lap time
except KeyboardInterrupt:
    # Handle the Command-F2 or Ctrl-C exception to keep its error message from displaying
    print('\nDone.')


import datetime
import time

# A unix epoch timstamp can be converted to a datetime object
# print(time.time())
# print(datetime.datetime.fromtimestamp(time.time()))
# print(datetime.datetime.fromtimestamp(10000000))
#
#
# dn = datetime.datetime.now()
# # return a datetime oject
# print(dn)
# print('year:{}, month:{}, day:{},'
# 	  ' hour:{}, minute:{}, second:{}'.format(dn.year,
# 						dn.month, dn.day, dn.hour, dn.minute, dn.second))
#
# dt = datetime.datetime(2017, 12, 20, 19, 16, 0)
# print('year:{}, month:{}, day:{},'
# 	  ' hour:{}, minute:{}, second:{}'.format(dt.year,
# 						dt.month, dt.day, dt.hour, dt.minute, dt.second))

# datetime objects can be compared, the later datetime object is the 'greated' value.
# springFestival = datetime.datetime(2018, 2, 16, 0, 0, 0)
# chrismas = datetime.datetime(2017, 12, 25, 0, 0, 0)
# dec252017 = datetime.datetime(2017, 12, 25, 0, 0, 0)
# #
# print(chrismas != dec252017)
# print(springFestival > chrismas)

# titedelta represents a duration of time rather than a moment in time
# delta = datetime.timedelta(weeks=1, days=11, hours=10, minutes=9, seconds=8)
# # to create a timedelta object
#
# print(delta.days, delta.seconds, delta.microseconds)
# print(delta.total_seconds())
#
# # passing a timedelta object to str() will return a nicely formated, human-readable string
# # representation fo the object
# print(str(delta))

# dt = datetime.datetime.now()
# thousandDays = datetime.timedelta(days=1000)
# print(dt + thousandDays)
# print(dt - thousandDays)


newYear2018 = datetime.datetime(2018, 1, 1)
while datetime.datetime.now() < newYear2018:
	time.sleep(1)

# Converting datetime object into string
# oct21st = datetime.datetime(2017, 10, 21, 16, 29, 0)
# print(oct21st.strftime('%Y/%m/%d %H:%M:%S'))
# print(oct21st.strftime('%I:%M %p'))
# print(oct21st.strftime("%B of '%y"))
#
#
# print(datetime.datetime.strptime('October 21, 2015', '%B %d, %Y'))
# print(datetime.datetime.strptime('2015/10/21 16:29:00', '%Y/%m/%d %H:%M:%S'))
# print(datetime.datetime.strptime("October of '15", "%B of '%y"))
# print(datetime.datetime.strptime("November of '63", "%B of '%y"))



import subprocess

# subprocess.run(['open', '/Applications/Calculator.app'])
# subprocess.run(['start', '/Applications/Calculator.app'], shell=True)

# subprocess.run(['open','/Applications/Textedit.app', 'poem.rtf'])

# import webbrowser
# webbrowser.open('https://study.163.com/course/courseMain.htm?courseId=1004106037')

# subprocess.run(['open', 'datetimeBasic.py'])
# subprocess.run(['open', 'poem.rtf'])


subprocess.run(["ls", "-l", "/Users/Smonkey/documents/python/pythonabc_online"])
subprocess.run(["ls", "-l", "/Users/Smonkey/documents/python/pythonabc_online"])


# subprocess.run(["ls", "-l", "/Users/Smonkey/documents/python/pythonabc_online"], stdout=subprocess.PIPE)才可以



# # countdown.py - A simple countdown script.
#
import time, subprocess

timeLeft = 5
while timeLeft > 0:
    print(timeLeft)
    time.sleep(1)
    timeLeft = timeLeft - 1

#
print('Music, Now!')
subprocess.run(['open', '像风一样自由.mp3'])

1 1 1 1 1 1 1 1 1 1 Rating 2.75 (4 Votes)