[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)

10.1 file

10.1.1 Created by open ()

--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()

10.1.2 Check that the file exists by exists ()


>>> 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

10.1.3 File type checking with isfile ()


>>> 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

10.1.4 Copy by copy ()


>>> import shutil
>>> shutil.copy("oops.txt","ohono.txt")
'ohono.txt'

10.1.5 Renaming the file with rename ()


>>> import os
>>> os.rename("ohono.txt","ohwel.txt")

10.1.6 Creating links with link () and symlink ()


#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

10.1.7 Changing permissions with chmod ()


#oops.Only read is allowed in txt.
>>> os.chmod("oops.txt",0o400)

>>> import stat
>>> os.chmod("oops.txt",stat.S_IRUSR)

10.1.8 Change to owner by chown ()


>>> uid=5
>>> gid=22
>>> os.chown("oops",uid,gid)

10.1.9 Getting the pathname with abspath ()


#Extend relative names to absolute names.
>>> os.path.abspath("oops.txt")
'/Users/practice/bin/oops.txt'

10.1.10 Getting the symbolic linkpath name with realpath ()

#Get the actual state of the link file.
>>> os.path.realpath("jeepers.txt")
'/Users/practice/bin/oops.txt'

10.1.11 Remove files with remove


>>> os.remove("oops.txt")
>>> os.path.exists("oops.txt")
False

10.2 directory

--Files are in a hierarchical structure of directories. A container for these files and directories is called a file system.

10.2.1 Created by mkdir ()

>>> os.mkdir("poems")
>>> os.path.exists("poems")
True

10.2.2 Deleted by rmdir ()


>>> os.rmdir("poems")
>>> os.path.exists("poems")
False

10.2.3 Creating a content list with listdir ()


>>> 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']

10.2.4 Changing the current directory with chdir ()

--You can move to another directory.


>>> import os
>>> os.chdir("poems")
>>> os.listdir(".")
['mcintyre']

10.2.5 Creating a list of files that match the pattern with glob ()

-* 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']

10.3 Programs and processes

--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

10.3.1 Creating a process with subprocess

--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

10.3.2 Creating a process by multiprocessing

--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

10.3.3 Force termination of process by terminate ()

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!

10.4 Calendar and clock


#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

10.4.1 datetime module

--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)

10.4.2 How to use the time module

--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

10.4.3 Reading and writing the date and time

--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']

10.5 Review questions

10-1 Write the current date in the form of a string in a text file called today.txt.


>>> from datetime import date
>>> with open("today.txt","wt") as fout:
...     now=date.today()
...     now_str=now.isoformat()
...     print(now_str,file=fout)

10-2 Let's read the contents of the text file today.txt into a string variable called today_string.


>>> with open("today.txt","rt") as input:
...     today_string=input.read()
... 
>>> today_string
'2020-01-28\n'

10-3 Let's analyze and retrieve the date from today_string.


>>> 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)

10-4 Make a list of files in the current directory.

>> 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`]

10-5 Make a list of files in the parent directory.


>>> import os
>>> os.listdir("..")
['.DS_Store', '61.py', 'bin', 'python', 'include', 'lib']


10-6 Let's create 3 separate processes using multiprocessing. Let each sleep for a random number of seconds, from 1 to 5 seconds.

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

10-7 Let's create a birthday date object.


>>> from datetime import date
>>> birthday=date(1994, 1, 1)
>>> birthday
datetime.date(1994, 1, 1)


10-8 What day was your birthday?

>>> import time
>>> from datetime import date
>>> birthday=date(1994, 1, 1)
>>> fmt="%Y-%m-%d-%A"
>>> date.strftime(birthday,fmt)
'1994-01-01-Saturday'


10-9 When will it be 10,000 days since I was born?


>>> from datetime import timedelta
>>> day=timedelta(days=10000)
>>> A=birthday+day
>>> A
datetime.date(2021, 5, 19)


Impressions

I reviewed it quickly. I want to see tutorials when using it.

References

"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"

Recommended Posts

[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Introduction to Python3 Day 23] Chapter 12 Become a Paisonista (12.1 to 12.6)
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python3 Day 3] Chapter 2 Py components: Numbers, strings, variables (2.2-2.3.6)
[Introduction to Python3 Day 2] Chapter 2 Py Components: Numbers, Strings, Variables (2.1)
Introduction to Effectiveness Verification Chapter 1 in Python
Introduction to Python language
Introduction to OpenCV (python)-(2)
[Introduction to Python3 Day 7] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.3-3.8)
[Introduction to Python3 Day 5] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.1-3.2.6)
[Introduction to Python3 Day 10] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.4-5.7)
[Introduction to Python3 Day 9] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.1-5.4)
[Introduction to Python3 Day 6] Chapter 3 Py tool lists, tuples, dictionaries, sets (3.2.7-3.2.19)
Introduction to effectiveness verification Chapter 3 written in Python
Introduction to Effectiveness Verification Chapter 2 Written in Python
Introduction to Python Django (2) Win
Introduction to serial communication [Python]
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
[Chapter 4] Introduction to Python with 100 knocks of language processing
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Udemy Python 3 + Application] 56. Closure
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Python] Let's use pandas
Introduction to regular expression processing system
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Python day 1
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
[AWS SAM] Introduction to Python version
[Python Tutorial] An Easy Introduction to Python
IPynb scoring system made with TA of Introduction to Programming (Python)
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 1
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 2
[Introduction to Udemy Python3 + Application] 18. List methods