------------ Traceback (most recent call last): File "/Users/lvrui/PycharmProjects/untitled/8/loadpickle.py", line 3, in <module> ret = pickle.load(open('db', 'rb')) AttributeError: Can't get attribute 'A' on <module '__main__' from '/Users/lvrui/PycharmProjects/untitled/8/loadpickle.py'>
# 源码 classThreadingMixIn: """Mix-in class to handle each request in a new thread."""
# Decides how threads will act upon termination of the # main process daemon_threads = False
defprocess_request_thread(self, request, client_address): """Same as in BaseServer but as a thread. In addition, exception handling is done here. """ try: self.finish_request(request, client_address) self.shutdown_request(request) except: self.handle_error(request, client_address) self.shutdown_request(request)
defprocess_request(self, request, client_address): """Start a new thread to process the request.""" t = threading.Thread(target = self.process_request_thread, args = (request, client_address)) t.daemon = self.daemon_threads t.start()
# 源码 classThreadingMixIn: """Mix-in class to handle each request in a new thread."""
# Decides how threads will act upon termination of the # main process daemon_threads = False
defprocess_request_thread(self, request, client_address): """Same as in BaseServer but as a thread. In addition, exception handling is done here. """ try: self.finish_request(request, client_address) self.shutdown_request(request) except: self.handle_error(request, client_address) self.shutdown_request(request)
defprocess_request(self, request, client_address): """Start a new thread to process the request.""" t = threading.Thread(target = self.process_request_thread, args = (request, client_address)) t.daemon = self.daemon_threads t.start()
# 源码 classBaseServer: ... defserve_forever(self, poll_interval=0.5): """Handle one request at a time until shutdown. Polls for shutdown every poll_interval seconds. Ignores self.timeout. If you need to do periodic tasks, do them in another thread. """ self.__is_shut_down.clear() try: # XXX: Consider using another file descriptor or connecting to the # socket to wake this up instead of polling. Polling reduces our # responsiveness to a shutdown request and wastes cpu at all other # times. with _ServerSelector() as selector: selector.register(self, selectors.EVENT_READ)
whilenot self.__shutdown_request: ready = selector.select(poll_interval) if ready: self._handle_request_noblock()
# 源码 classThreadingMixIn: """Mix-in class to handle each request in a new thread."""
# Decides how threads will act upon termination of the # main process daemon_threads = False
defprocess_request_thread(self, request, client_address): """Same as in BaseServer but as a thread. In addition, exception handling is done here. """ try: self.finish_request(request, client_address) self.shutdown_request(request) except: self.handle_error(request, client_address) self.shutdown_request(request)
defprocess_request(self, request, client_address): """Start a new thread to process the request.""" t = threading.Thread(target = self.process_request_thread, args = (request, client_address)) t.daemon = self.daemon_threads t.start()
# 源码 classBaseServer: ... def_handle_request_noblock(self): """Handle one request, without blocking. I assume that selector.select() has returned that the socket is readable before this function was called, so there should be no risk of blocking in get_request(). """ try: request, client_address = self.get_request() except OSError: return if self.verify_request(request, client_address): try: self.process_request(request, client_address) except: self.handle_error(request, client_address) self.shutdown_request(request) ...
# 源码 classThreadingMixIn: """Mix-in class to handle each request in a new thread."""
# Decides how threads will act upon termination of the # main process daemon_threads = False
defprocess_request_thread(self, request, client_address): """Same as in BaseServer but as a thread. In addition, exception handling is done here. """ try: self.finish_request(request, client_address) self.shutdown_request(request) except: self.handle_error(request, client_address) self.shutdown_request(request)
defprocess_request(self, request, client_address): """Start a new thread to process the request.""" t = threading.Thread(target = self.process_request_thread, args = (request, client_address)) t.daemon = self.daemon_threads t.start()
# 使用对象去操作数据库 # 分别去两个数据库中操作sql语句 my1.select('select uid, username, password from userinfo where age=26') my2.select('select uid, username, password from userinfo where age=26')
# 向已存在的压缩包中添加文件 z = zipfile.ZipFile('docs.zip', 'a') # 添加需要压缩的文件 z.write("test.conf") z.close()
# 解压缩所有文件 z = zipfile.ZipFile('docs.zip', 'r') z.extractall()
# 解压缩指定文件
# 列出压缩包中的所有文件名 z = zipfile.ZipFile('docs.zip', 'r') for i in z.namelist(): print(i) # 指定文件名称进行解压 z.extract("c1.py") z.close()
解压缩补充:
path 可以指定解压缩之后的文件的路径
pwd 可以指定去哪个目录下解压缩文件
1 2 3 4 5 6
defextract(self, member, path=None, pwd=None): """Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. `member' may be a filename or a ZipInfo object. You can specify a different directory using `path'. """
# 向已存在的压缩包中添加文件 t = tarfile.open('docs.zip', 'a') # 添加需要压缩的文件 t.add("test.conf") t.close()
# 解压缩所有文件 t = tarfile.open('docs.zip', 'r') t.extractall()
# 解压缩指定文件 # 列出压缩包中的所有文件名 t = tarfile.open('docs.zip', 'r') for i in t.getnames(): print(i)
# 指定文件名称进行解压 t.extract("tc2.py") t.close()
# 使用文件名获取该文件对象,指定该文件对象进行解压缩 o = t.getmember('tc2.py') t.extract(o) t.close()
解压缩补充:
tarfile解压缩单个文件时支持两种方式进行解压,一种是指定文件名,一种是指定文件对象。
解压方法支持解压缩文件到指定目录中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
defextractall(self, path=".", members=None, *, numeric_owner=False): """Extract all members from the archive to the current working directory and set owner, modification time and permissions on directories afterwards. `path' specifies a different directory to extract to. `members' is optional and must be a subset of the list returned by getmembers(). If `numeric_owner` is True, only the numbers for user/group names are used and not the names. """
defextract(self, member, path="", set_attrs=True, *, numeric_owner=False): """Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. `member' may be a filename or a TarInfo object. You can specify a different directory using `path'. File attributes (owner, mtime, mode) are set unless `set_attrs' is False. If `numeric_owner` is True, only the numbers for user/group names are used and not the names. """
>>> ret = subprocess.call(["df","-h"], shell=False) Filesystem Size Used Avail Use% Mounted on /dev/disk1 233G 107G 127G 46% / /dev/disk2s1 58G 36G 22G 63% /Volumes/SanDisk >>> print(ret) 0 ------------ # 以shell的模式运行 >>> ret = subprocess.call("df -h", shell=True) Filesystem Size Used Avail Use% Mounted on /dev/disk1 233G 107G 127G 46% / /dev/disk2s1 58G 36G 22G 63% /Volumes/SanDisk >>> print(ret) 0
check_call
检查执行命令后的返回值,如果是0则返回,如果不是则抛出异常
1 2 3 4 5 6
>>> ret = subprocess.check_call("exit 1", shell=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 584, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1