--The open () function not only opens the file, but also creates a new file when it requests a non-existent file.
>>> fout=open("oops.txt","wt")
>>> print("Oops,I created a file.",file=fout)
>>> fout.close()
>>> import os
>>> os.path.exists("oops.txt")
True
>>> os.path.exists("./oops.txt")
True
>>> os.path.exists("aaa.txt")
False
>>> os.path.exists(".")
True
>>> os.path.exists("..")
True
>>> name="oops.txt"
>>> os.path.isfile(name)
True
#Whether it is a directory isdir()use.
#"."Represents the current directory
#".."Represents the parent directory
>>> os.path.isdir(name)
False
>>> os.path.isdir(".")
True
>>> os.path.isdir("..")
True
#isabs()Returns whether the argument is an absolute path.
>>> os.path.isabs(name)
False
>>> os.path.isabs("/big/fake/name")
True
>>> os.path.isabs("/big/fake/name/without/a/leading/slash")
True
>>> os.path.isabs("big/fake/name/without/a/leading/slash")
False
>>> import shutil
>>> shutil.copy("oops.txt","ohono.txt")
'ohono.txt'
>>> import os
>>> os.rename("ohono.txt","ohwel.txt")
#yikes.oops from txt.Create a hard link to txt
>>> os.link("oops.txt","yikes.txt")
>>> os.path.isfile("yikes.txt")
True
jeepers.oops from txt.Create a symbolic link to txt
>>> os.symlink("oops.txt","jeepers.txt")
>>> os.path.islink("jeepers.txt")
True
#oops.Only read is allowed in txt.
>>> os.chmod("oops.txt",0o400)
>>> import stat
>>> os.chmod("oops.txt",stat.S_IRUSR)
>>> uid=5
>>> gid=22
>>> os.chown("oops",uid,gid)
#Extend relative names to absolute names.
>>> os.path.abspath("oops.txt")
'/Users/practice/bin/oops.txt'
#Get the actual state of the link file.
>>> os.path.realpath("jeepers.txt")
'/Users/practice/bin/oops.txt'
>>> os.remove("oops.txt")
>>> os.path.exists("oops.txt")
False
--Files are in a hierarchical structure of directories. A container for these files and directories is called a file system.
>>> os.mkdir("poems")
>>> os.path.exists("poems")
True
>>> os.rmdir("poems")
>>> os.path.exists("poems")
False
>>> os.mkdir("poems")
>>> os.listdir("poems")
[]
#Create subdirectory
>>> os.mkdir("poems/mcintyre")
>>> os.listdir("poems")
['mcintyre']
>>> fout=open("poems/mcintyre/the_good_man","wt")
>>> fout.write("""Cheerful and happy was his mood,
... He to the poor was kind and good,
... And he oft times did find them food,
... Also suppleis of coal and wood,
... """)
137
>>> fout.close()
>>> os.listdir("poems/mcintyre")
['the_good_man']
--You can move to another directory.
>>> import os
>>> os.chdir("poems")
>>> os.listdir(".")
['mcintyre']
-* Matches everything. -? Matches any single character. -[a, b, c] matches any of a, b, c. -[! A, b, c] matches characters other than a, b, c.
>>> import glob
>>> glob.glob("m*")
['mcintyre']
>>> glob.glob("??")
[]
>>> glob.glob("m??????e")
['mcintyre']
>>> glob.glob("[klm]*e")
['mcintyre']
--When you run a separate program, the operating system (OS) creates one ** process **. --The process is independent and cannot be peeked or disturbed.
--The OS manages all running processes. --Process data can also be accessed from the program itself → Use the os module of the standard library.
>>> import os
#Get the process id.
>>> os.getpid()
40037
#Get the current directory.
>>> os.getcwd()
'/Users/practice/bin/poems'
#Get user id.
>>> os.getuid()
501
#Get group id.
>>> os.getgid()
20
--You can start and stop other existing programs from Python by using the subprocess module of the standard library.
>>> import subprocess
>>> ret=subprocess.getoutput("date")
>>> ret
'Tuesday, January 28, 2020 11:17:59 JST'
>>> ret=subprocess.getoutput("date -u")
>>> ret
'Tuesday, January 28, 2020 02:19:40 UTC'
>>> ret=subprocess.getoutput("date -u | wc")
>>> ret
' 1 5 48'
#Accepts a list of commands and arguments.
>>> ret=subprocess.check_output(["date","-u"])
>>> ret
b'2020\xe5\xb9\xb4 1\xe6\x9c\x8828\xe6\x97\xa5 \xe7\x81\xab\xe6\x9b\x9c\xe6\x97\xa5 02\xe6\x99\x8222\xe5\x88\x8605\xe7\xa7\x92 UTC\n'
#A status code and output tuple are returned.
>>> ret=subprocess.getstatusoutput("date")
>>> ret
(0, 'Tuesday, January 28, 2020 11:26:35 JST')
#Call to receive only exit status()use.
>>> ret=subprocess.call("date")
Tuesday, January 28, 2020 11:27:05 JST
>>> ret
0
>>> ret=subprocess.call("date -u",shell=True)
Tuesday, January 28, 2020 02:30:49 UTC
>>> ret=subprocess.call(["date", "-u"])
Tuesday, January 28, 2020 02:34:41 UTC
--Use the multiprocessing module to execute Python functions as a separate process. You can even run multiple independent processes in one program.
mp.py
import multiprocessing
import os
def do_this(what):
whoami(what)
def whoami(what):
print("Process %s says: %s" % (os.getpid(), what))
#Process()The function starts a new process and executes what is specified by target by substituting the arguments specified by args.
#When you make an argument with args, if you do not put a comma at the end, it will be recognized as a character string and the result will be strange.
if __name__ == "__main__":
whoami("I'm the main program")
for n in range(4):
p = multiprocessing.Process(target=do_this, args=("I'm function %s" % n,))
p.start()
Execution result
python mp.py
Process 40422 says: I'm the main program
Process 40438 says: I'm function 0
Process 40439 says: I'm function 1
Process 40440 says: I'm function 2
Process 40441 says: I'm function 3
termina.py
import multiprocessing
import time
import os
def whoami(name):
print("I'm %s, in process %s" % (name, os.getpid()))
def loopy(name):
whoami(name)
start = 1
stop = 1000000
for num in range(start, stop):
print("\tNumber %s of %s. Honk!" % (num, stop))
time.sleep(1)
if __name__ == '__main__':
whoami("main")
p = multiprocessing.Process(target=loopy, args=("loopy" ,))
p.start()
time.sleep(5)
p.terminate()
Execution result
python termina.py
I'm main, in process 40518
I'm loopy, in process 40534
Number 1 of 1000000. Honk!
Number 2 of 1000000. Honk!
Number 3 of 1000000. Honk!
Number 4 of 1000000. Honk!
Number 5 of 1000000. Honk!
#Test if it is a leap year.
>>> import calendar
>>> calendar.isleap(1900)
False
>>> calendar.isleap(1996)
True
>>> calendar.isleap(1999)
False
>>> calendar.isleap(2000)
True
>>> calendar.isleap(2002)
False
>>> calendar.isleap(2004)
True
--The datetime module defines four main objects. --Date for date --Time for hours, minutes, seconds and fractions --Datetime for both date and time --Timedelta for date and time intervals
>>> from datetime import date
>>> halloween=date(2014,10,31)
>>> halloween
datetime.date(2014, 10, 31)
#It can be retrieved as an attribute.
>>> halloween.day
31
>>> halloween.month
10
>>> halloween.year
2014
#The content of date is isformat()Can be displayed with.
>>> halloween.isoformat()
'2014-10-31'
#today()Generate today's date using the method.
>>> from datetime import date
>>> now=date.today()
>>> now
datetime.date(2020, 1, 28)
>>> from datetime import timedelta
>>> one_day=timedelta(days=1)
>>> tomorrow=now + one_day
>>> tomorrow
datetime.date(2020, 1, 29)
>>> now + 17*one_day
datetime.date(2020, 2, 14)
>>> yesterday=now-one_day
>>> yesterday
datetime.date(2020, 1, 27)
#Use the time object of the datetime module to represent the time of day.
>>> from datetime import time
>>> noon=time(12,0,0)
>>> noon
datetime.time(12, 0)
>>> noon.hour
12
>>> noon.minute
0
>>> noon.second
0
>>> noon.microsecond
0
#datetime object is also isoformat()I have a method.
>>> from datetime import datetime
>>> some_day=datetime(2014,1,2,3,4,5,6)
>>> some_day
datetime.datetime(2014, 1, 2, 3, 4, 5, 6)
>>> some_day.isoformat()
'2014-01-02T03:04:05.000006'
#datetime gets the current date and time now()I have a method.
>>> from datetime import datetime
>>> x=datetime.now()
>>> x
datetime.datetime(2020, 1, 28, 13, 59, 8, 731311)
>>> x.year
2020
>>> x.day
28
>>> x.hour
13
>>> x.minute
59
>>> x.second
8
>>> x.microsecond
731311
#You can combine date and time objects with combine.
>>> from datetime import datetime,time,date
>>> noon=time(12)
>>> this_day=date.today()
>>> noon_today=datetime.combine(this_day,noon)
>>> noon_today
datetime.datetime(2020, 1, 28, 12, 0)
>>> noon_today.date()
datetime.date(2020, 1, 28)
>>> noon_today.time()
datetime.time(12, 0)
--There is a time module in addition to the time object of the datetime module. → The time () function of the time module returns Unix time.
--Unix time uses seconds from midnight, January 1, 1970.
>>> import time
>>> now=time.time()
>>> now
1580188038.071847
#ctime()You can use to convert Unix time to a string.
>>> time.ctime(now)
'Tue Jan 28 14:07:18 2020'
#localtime()Returns the date and time in standard time on the system.
>>> time.localtime(now)
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=28, tm_hour=14, tm_min=7, tm_sec=18, tm_wday=1, tm_yday=28, tm_isdst=0)
#gmttime()Returns the time in UTC.
>>> time.gmtime(now)
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=28, tm_hour=5, tm_min=7, tm_sec=18, tm_wday=1, tm_yday=28, tm_isdst=0)
#mktime()Is a struct_Converts a time object to Unix time.
>>> tm=time.localtime(now)
>>> time.mktime(tm)
1580188038.0
--The date and time output is not a proprietary patent of isoformat (). --strftime () is provided as a function of time module, datetime, date, time object and can be converted to a string. --Use strptime () to convert a string to date and time information.
#ctime()Converts Unix time to a string.
>>> import time
>>> now=time.time()
>>> time.ctime(now)
'Tue Jan 28 14:15:57 2020'
>>> import time
#strftime()Use the format specifier of.
>>> fmt="""It is %A,%B,%d,%Y, local time %I:%M:%S%p"""
>>> t=time.localtime()
>>> t
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=28, tm_hour=14, tm_min=21, tm_sec=10, tm_wday=1, tm_yday=28, tm_isdst=0)
#struct_Convert time to a string.
>>> time.strftime(fmt,t)
'It is Tuesday,January,28,2020, local time 02:21:10PM'
>>> from datetime import date
>>> some_day=date(2014,7,4)
#When used in a date object, only the date is formatted.
>>> some_day.strftime(fmt)
'It is Friday,July,04,2014, local time 12:00:00AM'
>>> from datetime import time
>>> some_time=time(10,45)
#When used in the time object, only the time is formatted.
>>> some_time.strftime(fmt)
'It is Monday,January,01,1900, local time 10:45:00AM'
#Conversely, strptime to convert a character string to date and time information()use.
#The parts other than the format child must match exactly.
>>> import time
>>> fmt="%Y-%m-%d"
>>> time.strptime("2012-01-29",fmt)
time.struct_time(tm_year=2012, tm_mon=1, tm_mday=29, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=29, tm_isdst=-1)
#setlocale()Will return the date in the specified locale.
>>> import locale
>>> from datetime import date
>>> halloween=date(2014,10,31)
>>> for lang_country in ["en_us","fr_fr","de_de","es_es","is_is",]:
... locale.setlocale(locale.LC_TIME,lang_country)
... halloween.strftime("%A, %B, %d")
...
'en_us'
'Friday, October, 31'
'fr_fr'
'Vendredi, octobre, 31'
'de_de'
'Freitag, Oktober, 31'
'es_es'
'viernes, octubre, 31'
'is_is'
'föstudagur, október, 31'
>>> import locale
>>> names=locale.locale_alias.keys()
>>> good_names=[name for name in names if
... len(name)==5 and name[2]=="_"
...
...
... ]
>>> good_names[5]
'ak_gh'
>>> good_names=[name for name in names if
... len(name)==5 and name[2]=="_"]
>>> good_names[5]
'ak_gh'
>>> good_names[:5]
['a3_az', 'aa_dj', 'aa_er', 'aa_et', 'af_za']
>>> de=[name for name in good_names if name.startswith("de")]
>>> de
['de_at', 'de_be', 'de_ch', 'de_de', 'de_it', 'de_lu']
>>> from datetime import date
>>> with open("today.txt","wt") as fout:
... now=date.today()
... now_str=now.isoformat()
... print(now_str,file=fout)
>>> with open("today.txt","rt") as input:
... today_string=input.read()
...
>>> today_string
'2020-01-28\n'
>>> import time
>>> fmt="%Y-%m-%d\n"
#String ⇨ strptime for date information()use.
>>> time.strptime(today_string,fmt)
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=28, tm_isdst=-1)
>> import os
>>> os.listdir(".")
[`flask2.py`, `pip3.7`, `jeepers.txt`, `villains`, `activate.ps1`, `index.html`, `.DS_Store`, `python3`, `settings.cfg`, `relativity`, `yikes.txt`, `easy_install`, `python`, `pip3`, `ohwel.txt`, `today.txt`, `activate.fish`, `easy_install-3.7`, `bottle3.py`, `zoo.py`, `flask9-2.py`, `test1.py`, `weather.py`, `bottle2.py`, `wheel`, `enterprise.db`, `flask9-3.py`, `links.py`, `__pycache__`, `flask3a.py`, `python3.7`, `test2.py`, `bottle1.py`, `python-config`, `activate_this.py`, `pip`, `bottle_test.py`, `definitions.db`, `flask3b.py`, `menu.xml`, `books.csv`, `mp.py`, `activate.xsh`, `weatherman.py`, `lelatibity`, `termina.py`, `bfile`, `flask3c.py`, `relatibity`, `flask9-5.py`, `sources`, `text.txt`, `templates`, `activate`, `poems`, `books.db`, `flask1.py`, `mcintyre.yaml`, `report.py`, `zoo.db`, `activate.csh`]
>>> import os
>>> os.listdir("..")
['.DS_Store', '61.py', 'bin', 'python', 'include', 'lib']
multi_times.py
import multiprocessing
import os
def now(seconds):
from datetime import datetime
from time import sleep
sleep(seconds)
print("wait", seconds, "second, time is ", datetime.utcnow())
if __name__== "__main__":
import random
for n in range(3):
seconds=random.random()
proc=multiprocessing.Process(target=now, args=(seconds,))
proc.start()
Execution result
python3 multi_times.py
wait 0.31146317121366796 second, time is 2020-01-28 06:30:39.446188
wait 0.5162984978514253 second, time is 2020-01-28 06:30:39.650398
wait 0.8005158157521853 second, time is 2020-01-28 06:30:39.934936
>>> from datetime import date
>>> birthday=date(1994, 1, 1)
>>> birthday
datetime.date(1994, 1, 1)
>>> import time
>>> from datetime import date
>>> birthday=date(1994, 1, 1)
>>> fmt="%Y-%m-%d-%A"
>>> date.strftime(birthday,fmt)
'1994-01-01-Saturday'
>>> from datetime import timedelta
>>> day=timedelta(days=10000)
>>> A=birthday+day
>>> A
datetime.date(2021, 5, 19)
I reviewed it quickly. I want to see tutorials when using it.
"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"
Recommended Posts