Common syntax for python operation and maintenance

os.path.exists('/data') Determine whether directories and files exist, and determine whether the current path exists

os.path.isdir('/data') Determine whether it is a directory
os.path.isfile('/data/1.txt') Determine whether it is a file
os.access('/data/a', os.F_OK ) To judge whether the file or directory exists, os.R_OK reads os.X_OK and executes
os.remove('/home/test/1.txt') to delete the file
os.listdir('/home/test/') All files form a list
os.rename(old, new) Modify the file or directory name
time.strftime('%Y-%m-%d') Output the year, month and day
if os.system(cmd) == 0: Determine whether the command is execution succeed

[2*i for i in range(1,9) if i > 2] Determine if i is greater than 2, then multiply by 2 to generate a list
output [5, 6, 7, 8, 9, 10]
when certain Condition ( if i > 2 ), we perform the specified operation ( 2*i ), when certain conditions are met ( if i > 2 ), we perform the specified operation ( 2*i ), in order to obtain a new listings

d = {'name': 'cby', 'age': 18}
dict([(v, k) for k, v in d.items()]) #Invert the dictionary k, v and
output {18: ' age', 'cby': 'name'}

The assert statement is used to assert that something is true. Say for example that you're pretty sure that the list you're working with
contains one element, and you want to confirm that, and if it's not true, throw an error, the assert statement is
ideal for situations like this. When a statement assertion fails, an AssertionError will be thrown.
In [79]: c = [6, 4, 3, 2, 1]

In [80]: assert len(c) >= 1

In [81]: assert len(c) <= 1
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-81-3dd2022e1943> in <module>()
----> 1 assert len(c) <= 1

AssertionError:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325019157&siteId=291194637