如何用Python获取Linux用户的$HOME目录?
搜索关键字:linux python get $HOME
参考解答:
import os os.path.expanduser('~') import os, pwd pwd.getpwuid(os.getuid()).pw_dir import os os.getenv('HOME')
参考链接:
- https://docs.python.org/2/library/os.path.html#os.path.expanduser
- http://stackoverflow.com/questions/2668909/how-to-find-the-real-user-home-directory-using-python
- http://stackoverflow.com/questions/4028904/how-to-get-the-home-directory-in-python
如何用Python获取主机名(hostname)?
搜索关键字:python get hostname
import socket print socket.gethostname() import platform platform.node() import os os.getenv('HOSTNAME') os.environ['HOSTNAME'] import os myhost = os.uname()[1]
参考链接:
http://stackoverflow.com/questions/4271740/how-can-i-use-python-to-get-the-system-hostname
用Python获取日期字符串
import datetime yesterday = str(datetime.date.today() - datetime.timedelta(days=1))
Python的try catch finally用法
搜索关键字:
- python file open error try finally
- python try except finally
参考链接:
- http://stackoverflow.com/questions/8774830/how-with-is-better-than-try-catch-to-open-a-file-in-python
- http://stackoverflow.com/questions/7777456/python-tryexceptfinally
- https://docs.python.org/2/tutorial/errors.html
- https://wiki.python.org/moin/HandlingExceptions
- http://ixyzero.com/blog/archives/1594.html
- =
- http://stackoverflow.com/questions/9282967/how-to-open-a-file-using-the-open-with-statement
Python的 socket.gethostbyname_ex 的异常如何捕获?
- https://docs.python.org/2/library/socket.html#socket.gaierror #except socket.gaierror:
- http://stackoverflow.com/questions/13413258/how-do-i-catch-an-exception-for-a-module-that-ive-not-fully-imported
==
- https://docs.python.org/2/library/stdtypes.html#str.join
- https://docs.python.org/2/library/string.html#string.join
- =
- Powerful Python One-Liners – Python Wiki
==
Python中的某一行太长怎么办?
搜索关键字:
site:stackoverflow.com Python multi lines
参考链接:
- http://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string
- http://stackoverflow.com/questions/14417571/breaking-a-line-of-python-to-multiple-lines
- http://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python
- http://stackoverflow.com/questions/4172448/is-it-possible-to-break-a-long-line-to-multiple-lines-in-python
- http://seanlin.logdown.com/posts/210861-python-idioms-8-too-long
- https://docs.python.org/2/reference/lexical_analysis.html#implicit-line-joining
- http://stackoverflow.com/questions/181530/python-style-multiple-line-conditions-in-ifs
以HTML形式查看pydoc的方法
CMD_prompt> python -m pydoc -p 7777
7777为本机端口号,也可改成其他端口号;然后浏览器中访问 http://localhost:{端口号}/ ,如 http://localhost:7777/ 即可
命令行解释:
- -m pydoc #打开pydoc模块,pydoc是查看python文档的首选工具
- -p 7777 #表示在7777端口上启动server
检查 字符串needle 是否有特定的结尾
惯用(生成器表达式):
if any(needle.endswith(e) for e in ('ly', 'ed', 'ing', 'ers')): print('Is valid') else: print('Invalid')
非惯用(列表生成式):
if any([needle.endswith(e) for e in ('ly', 'ed', 'ing', 'ers')]): print('Is valid') else: print('Invalid')
Python反模式:从”坏代码”中学习最佳实践
http://docs.quantifiedcode.com/python-anti-patterns/index.html
=-=
StackExchange上和Python相关的一些问答
- http://programmers.stackexchange.com/questions/tagged/python?sort=votes&pageSize=15
- http://programmers.stackexchange.com/questions/tagged/algorithms?sort=votes&pageSize=15
- http://programmers.stackexchange.com/questions/tagged/programming-practices?sort=votes&pageSize=15
- http://programmers.stackexchange.com/questions/tagged/database-design?sort=votes&pageSize=15
- http://programmers.stackexchange.com/questions/tagged/security?sort=votes&pageSize=15
合理使用Python对象提高性能、速度
搜索关键字:
- Python Complexity
- Python speed Complexity
- Python performance Complexity
- Python Efficiency
- Python idioms performance
- Python idioms speed
- Python idioms Complexity
参考链接:
- Python Idioms and Efficiency 1/28/07 by Patrice Neff – Memonic
- PythonSpeed – Python Wiki
- PythonSpeed/PerformanceTips
- TimeComplexity – Python Wiki
- Complexity of Python Operations
- =
- http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/
- http://stackoverflow.com/questions/7165465/optimizing-python-code
- =
- http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python
《“Python的一些小知识点_10”》 有 1 条评论
一行 Python 能实现什么丧心病狂的功能?
https://www.zhihu.com/question/37046157