-
os.path.exists to check if file or directory exists.
import os os.path.exists('/tmp/1.txt') os.path.exists('/tmp')
-
os.path.isfile to check if file exists.
import os os.path.isfile('/tmp/1.txt')
import os os.path.exists('/tmp/1.txt') os.path.exists('/tmp')
import os os.path.isfile('/tmp/1.txt')
import os os.path.abspath('abc/abc.txt')
s='abc' print(s.index('b')) # 1 print(s.find('b')) # 1 print(s.find('d')) # -1 print(s.index('d')) # exception
with open('myfile.txt') as f: return [line for line in f if len(line)>80]
class Iterator(object): def next(self): pass class Iterable(object): def __iter__(self): return Iterator()
class AClass(object): def __init__(self): self._list = [1,2,3] def __len__(self): return len(self._list) a=AClass() print(a.__len__()) # 3 print(len(a)) # 3
class AClass(object): def __init__(self): pass @staticmethod def a(): AClass.b() @staticmethod def b(): pass