In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail about the commonly used python dates, logs, access to content cycle code snippet is what, the content of the article is of high quality, so the editor to share with you to do a reference, I hope you have a certain understanding of the relevant knowledge after reading this article.
Recently combed the shell script and python, some of the commonly used content in the script, consider a variety of ways to sort out, to form a useful code snippet, so that it can be used directly when needed, can also be used for memo and thinking.
The code snippets are as follows: the common acquisition methods of date and time in python; the use of logging module for recording and processing logs; and obtaining loop conditions from directories, files, and naming results to cycle.
I've compiled these useful code snippets into a Python script, and the tests are available.
The script reads as follows:
#! / usr/bin/env python
# _ * _ coding:utf8_*_
# commonly used date and time
Import datetime,time
Today = datetime.date.today ()
Yesterday = datetime.date.today ()-datetime.timedelta (days=1)
Tomorrow = datetime.date.today () + datetime.timedelta (days=1)
Today_nyr = int (datetime.datetime.strftime (today,'% Y% m% d'))
Yesterday_nyr = int (datetime.datetime.strftime (yesterday,'% Y% m% d'))
Tomorrow_nyr = int (datetime.datetime.strftime (tomorrow,'% Y% m% d'))
Time.strftime ('% Y-%m-%d% HRV% MVA% Swatch, time.localtime (time.time ()
Print "now time is {time}" .format (time=time.strftime ('% Y-%m-%d% HV% MV% Swatch, time.localtime (time.time ()
Print "normal type, today is {today}, yesterday is {yesterday}, tomorrow is {tommorrow}." .format (today=today,yesterday=yesterday,tommorrow=tomorrow)
Print "nyr style, today is {today}, yesterday is {yesterday}, tommrrow is {tommorrow}." .format (today=Today_nyr,yesterday=Yesterday_nyr,tommorrow=Tomorrow_nyr)
# logging.info ("now time is {time}" .format (time=time.strftime ('% Y-%m-%d% Htime% MVR% slots, time.localtime (time.time ())
# logging.info ("normal type, today is {today}, yesterday is {yesterday}, tomorrow is {tommorrow}." .format (today=today,yesterday=yesterday,tommorrow=tomorrow))
# logging.info ("nyr style, today is {today}, yesterday is {yesterday}, tommrrow is {tommorrow}." .format (today=Today_nyr,yesterday=Yesterday_nyr,tommorrow=Tomorrow_nyr))
# use of Program Log Module logging
Import logging
Import os
THISFILEPATH = os.path.dirname (os.path.realpath (_ _ file__))
Logfile ='{path} / python_test_file.log'.format (path=THISFILEPATH)
Logging.basicConfig (level=logging.DEBUG
Format='% (asctime) s -% (filename) s-[line:% (lineno) d]% (levelname) s:% (message) s'
Datefmt='%Y-%m-%d H:%M:%S p'
Filename=logfile
# level=10
Filemode='a')
Logging.info ("This is an info.\ n")
Logging.warn ("This is a warning.\ n")
Logging.error ("This is an error.\ n")
# logging.log ("This is a log.\ n\ n")
Logging.debug ("This is a debug.\ n\ n")
Logging.critical ("This is a critical\ n\ n\ n")
Logging.info ("now time is {time}" .format (time=time.strftime ('% Y-%m-%d% HV% MVR% slots, time.localtime (time.time ())
Logging.info ("normal type, today is {today}, yesterday is {yesterday}, tomorrow is {tommorrow}." .format (today=today,yesterday=yesterday,tommorrow=tomorrow))
Logging.info ("nyr style, today is {today}, yesterday is {yesterday}, tommrrow is {tommorrow}." .format (today=Today_nyr,yesterday=Yesterday_nyr,tommorrow=Tomorrow_nyr))
# retrieve all file names from a directory and loop them
From subprocess import call
File_Dir = "/ tmp"
File_List = os.listdir (File_Dir)
For f in File_List:
If (f [0] = ='.'):
Continue
Else:
Logging.info ("{filename}" .format (filename=f))
# read multiple lines of a file and cycle
# File reading method 1:
File = open ("/ tmp/3.txt")
While 1:
Line = file.readline ()
If not line:
Break
Logging.info ("this line is: {line}" .format (line=line))
# File reading method 2:
Import fileinput
For line in fileinput.input ("/ tmp/3.txt"):
Logging.info ("this line is: {line}." .format (line=line))
# method 3 for reading files:
File = open ("/ tmp/3.txt")
For line in file:
Logging.info ("this line is: {line}." .format (line=line))
# obtain the execution result of a shell command and cycle
# get command method 1:
Import subprocess
Shell_cmd = "df-h | awk'{print $1}'"
P = subprocess.Popen ("{cmd}" .format (cmd=shell_cmd), shell=True, stdout=subprocess.PIPE)
Out = p.stdout.readlines ()
For line in out:
Print line.strip ()
Logging.info ("file system is: {line}." .format (line=line.strip ()
# method 2 for obtaining commands:
For line in subprocess.Popen ("df-h | awk'{print $1}'", shell=True,stdout=subprocess.PIPE) .stdout.readlines ():
Print line.strip ()
Logging.info ("file system is: {line}." .format (line=line.strip ()
The above script can be executed directly using python 3.py.
The execution result of the script is as follows:
2017-03-02 16:48:43 PM-3.py-[line:38] INFO: This is an info.
2017-03-02 16:48:43 PM-3.py-[line:39] WARNING: This is a warning.
2017-03-02 16:48:43 PM-3.py-[line:40] ERROR: This is an error.
2017-03-02 16:48:43 PM-3.py-[line:42] DEBUG: This is a debug.
2017-03-02 16:48:43 PM-3.py-[line:43] CRITICAL: This is a critical
2017-03-02 16:48:43 PM-3.py-[line:46] INFO: now time is 2017-03-02 16:48:43
2017-03-02 16:48:43 PM-3.py-[line:47] INFO: normal type, today is 2017-03-02, yesterday is 2017-03-01, tomorrow is 2017-03-03.
2017-03-02 16:48:43 PM-3.py-[line:48] INFO: nyr style, today is 20170302, yesterday is 20170301, tommrrow is 20170303.
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: 40001501_20170302.csv
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: proc_cpu_result
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: qaucli_022317-18_15_41.log
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: sfcbHttpSocket
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: 1.py
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: sfcbLocalSocket
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: 2.txt
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: null
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: 3.txt
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: 3.py
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: hsperfdata_root
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: updatehbaconf.sh.log
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: proc_cpu_detail.txt
2017-03-02 16:48:43 PM-3.py-[line:60] INFO: python_test_file.log
2017-03-02 16:48:43 PM-3.py-[line:69] INFO: this line is: 1
2017-03-02 16:48:43 PM-3.py-[line:69] INFO: this line is: 2
2017-03-02 16:48:43 PM-3.py-[line:69] INFO: this line is: 3
2017-03-02 16:48:43 PM-3.py-[line:69] INFO: this line is: 4
2017-03-02 16:48:43 PM-3.py-[line:69] INFO: this line is: 5
2017-03-02 16:48:43 PM-3.py-[line:69] INFO: this line is: 6
2017-03-02 16:48:43 PM-3.py-[line:69] INFO: this line is: 7
2017-03-02 16:48:43 PM-3.py-[line:69] INFO: this line is: 8
2017-03-02 16:48:43 PM-3.py-[line:69] INFO: this line is: 9
2017-03-02 16:48:43 PM-3.py-[line:74] INFO: this line is: 1
.
2017-03-02 16:48:43 PM-3.py-[line:74] INFO: this line is: 2
.
2017-03-02 16:48:43 PM-3.py-[line:74] INFO: this line is: 3
.
2017-03-02 16:48:43 PM-3.py-[line:74] INFO: this line is: 4
.
2017-03-02 16:48:43 PM-3.py-[line:74] INFO: this line is: 5
.
2017-03-02 16:48:43 PM-3.py-[line:74] INFO: this line is: 6
.
2017-03-02 16:48:43 PM-3.py-[line:74] INFO: this line is: 7
.
2017-03-02 16:48:43 PM-3.py-[line:74] INFO: this line is: 8
.
2017-03-02 16:48:43 PM-3.py-[line:74] INFO: this line is: 9
.
2017-03-02 16:48:43 PM-3.py-[line:79] INFO: this line is: 1
.
2017-03-02 16:48:43 PM-3.py-[line:79] INFO: this line is: 2
.
2017-03-02 16:48:43 PM-3.py-[line:79] INFO: this line is: 3
.
2017-03-02 16:48:43 PM-3.py-[line:79] INFO: this line is: 4
.
2017-03-02 16:48:43 PM-3.py-[line:79] INFO: this line is: 5
.
2017-03-02 16:48:43 PM-3.py-[line:79] INFO: this line is: 6
.
2017-03-02 16:48:43 PM-3.py-[line:79] INFO: this line is: 7
.
2017-03-02 16:48:43 PM-3.py-[line:79] INFO: this line is: 8
.
2017-03-02 16:48:43 PM-3.py-[line:79] INFO: this line is: 9
.
2017-03-02 16:48:43 PM-3.py-[line:90] INFO: file system is: Filesystem.
2017-03-02 16:48:43 PM-3.py-[line:90] INFO: file system is: / dev/sda2.
2017-03-02 16:48:43 PM-3.py-[line:90] INFO: file system is: tmpfs.
2017-03-02 16:48:43 PM-3.py-[line:90] INFO: file system is: / dev/sda1.
2017-03-02 16:48:43 PM-3.py-[line:90] INFO: file system is: / dev/sda5.
2017-03-02 16:48:43 PM-3.py-[line:96] INFO: file system is: Filesystem.
2017-03-02 16:48:43 PM-3.py-[line:96] INFO: file system is: / dev/sda2.
2017-03-02 16:48:43 PM-3.py-[line:96] INFO: file system is: tmpfs.
2017-03-02 16:48:43 PM-3.py-[line:96] INFO: file system is: / dev/sda1.
2017-03-02 16:48:43 PM-3.py-[line:96] INFO: file system is: / dev/sda5.
On the commonly used python date, log, get content cycle code snippet is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.