Common class libraries for Python knowledge points

Keywords: Python Programming Pycharm Programmer AI

1. What is a time tuple?

The processing time of 9 sets of numbers assembled by many Python functions in one meta:



The above is struct_time tuple. This structure has the following properties:


2. Use datetime to get today's date and the date N days before

Q: how do I get today's date in Python? How to get the date of the previous N days?

A: the datetime module can be used to complete the date acquisition task.

(1) Get the date of the day

import datetime
# Get the date of the day
# The strftime() function outputs the time information to the desired format, such as' 2020-08-03 'date_today ='%Y-%m-%d')
# Output date data with hours and minutes_ today2 ='%Y-%m-%d %H-%M-%S')

(2) Get the date N days before

# Complete the date with the timedelta(N) function_ 3today_ ago = ( -datetime.timedelta(3)).strftime('%Y-%m-%d')

3. Get floating point time (time) in seconds:


>>> import time>>> print time.time()
#Gets the floating-point value of the current time in seconds 1369031293.33 > > >

4. Get the time that people can intuitively understand (CTime):

print time.ctime()Mon May 20 14:29:30 2013
#Get intuitive time that people can understand

5. Convert floating point time to visual time:

>>> t = time.time()
#Floating point time > > > 
print t1369034676.69>>> 
print time.ctime(t)
#Convert floating point time to intuitive time Mon May 20 15:24:36 2013

6. Obtain Coordinated Universal Time (UTC) format:

>>> print time.gmtime()
#Get the current time in UTC format, time.struct_time(tm_year=2013, 
tm_mon=5, tm_mday=20, tm_hour=6,tm_min=37, tm_sec=45, tm_wday=0, tm_yday=140, tm_isdst=0)

7. Time to convert UTC format time to floating point value:

>>> gmt = time.gmtime()
#Time in UTC > > > 
print gmttime.struct_time(tm_year=2013, tm_mon=5, tm_mday=20, 
tm_hour=6,tm_min=48, tm_sec=13, tm_wday=0, tm_yday=140, tm_isdst=0)>>> 
print time.mktime(gmt)
#Time to convert UTC format time to floating point value 1369003693.0 > > > LT = time. Localtime ()
#Change UTC format to current time zone current time > > > 
print lttime.struct_time(tm_year=2013, tm_mon=5, tm_mday=20, tm_hour=14,
tm_min=49, tm_sec=25, tm_wday=0, tm_yday=140, tm_isdst=0)>>> 
print time.mktime(lt)#
#Time 1369032565.0 to convert UTC format time to floating point value

8.strptime and strftime functions

time.strftime(Time format)datetime.strptime(character string,Time format)
demonstration:datetime.strptime(str,'%Y-%m-%d')"%Y-%m-%d %H:%M:%S")

9. Return the datetime object of the current date and time in the region
# Output: datetime.datetime(2019, 12, 9, 13, 27, 54, 693978)

10. Return array: (year, week, day of week)

d = datetime(2019,12,6,13,30,50)d.isocalendar()
# Output: (2019, 49, 5)

11. How to delete a file in Python?

Use os.remove(filename) or os.unlink(filename)

12. How does Python copy a file?

The shutil module has a copyfile function to copy files

13. How does Python open a file?


14. How does Python rename a file?

os.rename(current_file_name, new_file_name)

15. How does Python create a directory?


16. How does Python delete a directory?


17. How does Python locate files?

The tell() method tells you the current location in the file. In other words, the next read and write will occur after so many bytes at the beginning of the file.

The seek (offset [,from]) method changes the position of the current file. The Offset variable indicates the number of bytes to be moved. The From variable specifies the reference position to start moving bytes.

If from is set to 0, it means that the beginning of the file is used as the reference position for moving bytes. If it is set to 1, the current position is used as the reference position. If it is set to 2, the end of the file will be used as the reference position.

18. How does Python read keyboard input?

raw_input function

The raw_input([prompt]) function reads a line from the standard input and returns a string (excluding the ending newline character).

input function

input([prompt]) function and raw_input([prompt]) The function is basically similar, but input can receive a Python expression as input and return the operation result. Finally, if you are not very tight in time and want to improve quickly, the most important thing is not afraid of hardship, I suggest you can contact dimension: 762459510, which is really good. Many people make rapid progress and need you to be not afraid of hardship! You can go Add a look~

19. How does Python close a file?

The close () method of the File object flushes any information that has not been written in the buffer and closes the File. After that, it can no longer be written.

When the reference of a file object is reassigned to another file, Python closes the previous file. It is a good habit to close the file with the close () method.

20. How does Python write data to a file?

The write() method of the File object can write any string to an open File. It should be noted that Python strings can be binary data, not just text.

The write() method does not add a newline ('\ n') at the end of the string:

21. How does Python read data from a file?

The read () method of the File object reads a string from an open File. It is important to note that Python strings can be binary data, not just text.

Posted by JTapp on Sat, 25 Sep 2021 00:44:11 -0700