Sys module
Sys. argv: Implements passing parameters from outside the program to the program.
The positional argument argv[0] represents the py file itself, and runs the method python xx.py with arguments 1 and 2.
self = sys.argv[0]
name = sys.argv[1]
age = sys.argv[2]
print self, name, age
Copy the code
Sys.getdefaultencoding (): Gets the current encoding of the system, usually ASCII by default.
print sys.getdefaultencoding()
Copy the code
Sys.setdefaultencoding (): Sets the system default coding,
If dir (sys) doesn’t work, you can reload(sys) and setDefaultencoding (‘utf8’), then the defaultencoding is set to UTF8. (python2.7 may require this)
reload(sys)
sys.setdefaultencoding('utf8')
Copy the code
Sys. path: Gets a collection of strings for the specified module search path
sys.path
Copy the code
Sys. platform: obtains the current system platform.
print sys.platform
Copy the code
sys.exit()
Function: At the end of the main program, the interpreter automatically exits, but if you need to exit the program halfway through, you can call sys.exit, which is returned to the calling program with an optional integer argument, meaning you can catch calls to sys.exit in the main program. (0 is normal exit, other is exception)
for i in range(1, 10):
print '%s time :' % i, i
if i == 5:
print 'Fifth exit'
sys.exit(0)
Copy the code
OS module
Os.name () — Determine which platform is being used, Windows returns’ nt’; Linux returns’ posix ‘
print os.name()
Copy the code
Os.getcwd () — get the current working directory.
print os.getcwd()
Copy the code
Os.listdir () — Specifies all files and directory names under all directories.
print os.listdir('. ')
Copy the code
4. Os.remove () — Removes the specified file
os.remove('aaa.txt')
Copy the code
Os.rmdir () — Deletes the specified directory
os.rmdir('C://Users/xiaoxinsoso/Desktop/aaa')
Copy the code
6. Os.mkdir () — Create a directory, note: this can only create a single layer, to build recursively: os.makedirs()
os.makedirs('aaa/aaa')
Copy the code
Os.path.isfile () — Checks whether the specified object is a file. Returns True if True, False otherwise
print os.path.isfile('ccc.txt')
print os.path.isfile('aaa')
Copy the code
Os.path.isdir () — Checks whether the specified object is a directory. Is True, otherwise False. Ex. :
print os.path.isdir('aaa')
print os.path.isdir('ccc.txt')
Copy the code
9. Os.path.exists () — Checks whether the specified object exists. True, otherwise False. Example:
print os.path.exists('bbb')
print os.path.exists('aaa')
print os.path.exists('ccc.txt')
Copy the code
10.os.path.split () — Returns the directory and file name of the path. Ex. :
print os.path.split('C://Users/xiaoxinsoso/Desktop/aaa/ccc.txt')
Copy the code
Os.getcwd () — Get the current working directory
print os.getcwd()
Copy the code
Os.system () — Execute the shell command.
Note: When running shell commands here, if you want to call a previous Python variable, you can do the following:
var = 123
os.environ['var'] = str(var) # note that [] must be "string"
os.system('echo $var')
os.system('dir')
Copy the code
13. Os.chdir () — Change the directory to the specified directory
Os.path.getsize () — Gets the size of the file, or 0 if it is a directory
print os.path.getsize('ccc.txt')
Copy the code
Os.path.abspath () — Get the absolute path. Ex. :
print os.path.abspath('. ')
Copy the code
Os.path. join(path, name) — Join directory and file name. Ex. :
print os.path.join('c://user/xiaoxinsoso/'.'wenjian.txt')
Copy the code
Os.path.basename (path) — Returns the file name
print os.path.basename('ccc.txt')
Copy the code
Os.path.dirname (path) — Returns the file path
print os.path.dirname('C://Users/xiaoxinsoso/Desktop/aaa/ccc.txt')
Copy the code
19. Get the actual directory where the program resides
if __name__ == "__main__":
print os.path.realpath(sys.argv[0])
print os.path.split(os.path.realpath(sys.argv[0]))
print os.path.split(os.path.realpath(sys.argv[0]))[0]
Copy the code
Time module
ticks = time.time()
print "The current timestamp is :", ticks
Copy the code
Get the current time
localtime = time.localtime(time.time())
print "Local time is :", localtime
Copy the code
Gets the time of formatting
localtime = time.asctime(time.localtime(time.time()))
print "Local time is :", localtime
Copy the code
Formatting date
Format it into the 2017-01-22 16:36:27 format
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
Copy the code
Format to Sun Jan 22 16:36:27 2017
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
Copy the code
Converts a format string to a timestamp
a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a, "%a %b %d %H:%M:%S %Y"))
Copy the code
Get the calendar of the month
cal = calendar.month(2017, 1)
print "Output the calendar for January 2016 :"
print cal
Copy the code
Datetime module
Datetime Indicates the time type
now = datetime.datetime.now()
print now
now = date time.datetime.now()
yes_time = now + date time.timedelta(days=-1) # The day before
Copy the code
Turn a datetime string
strdatetime = now.strftime("%Y-%m-%d %H:%M:%S") # Display time as a string
strdatetime1= now.strftime("%Y-%m-%d") # Display the time as a string, only the date
print strdatetime
print strdatetime1
Copy the code
The string to turn a datetime
datetime1 = datetime.datetime.strptime(strdatetime1, "%Y-%m-%d")
print datetime1
Copy the code
Datetime to timestamp
time_time = time.mktime(datetime1.timetuple())
print time_time
Copy the code
Timestamp to string
time1 = time.strftime('%Y-%m-%d',time.localtime(time_time))
print time1
Copy the code
Turn the date a datetime
date1 = datetime.date(2012, 11, 19)
date = datetime.date.today()
print date
print datetime.datetime.strptime(str(date),'%Y-%m-%d') Convert date to STR, convert STR to datetime
print datetime.datetime.strptime(str(date1),'%Y-%m-%d') Convert date to STR, convert STR to datetime
Copy the code