Skip to content

Instantly share code, notes, and snippets.

@liuyix
Last active December 15, 2015 17:59
Show Gist options
  • Save liuyix/5300279 to your computer and use it in GitHub Desktop.
Save liuyix/5300279 to your computer and use it in GitHub Desktop.
Python学习备忘

#Python Tips

调用外部命令

detect function type

read large file

python脚本中执行命令行

os.system('cmd')

如果不想要结果输出,可以使用下面的代码

neveroutput = os.popen('your cmd').read()

how to jump to a particular line in a huge text file?

Credit: http://stackoverflow.com/q/620367

Python doc: linecache

enumerate(fp) is a better solution.

http://stackoverflow.com/a/2081880

To Conclude: Use fileobject.readlines() or for line in fileobject as a quick solution for small files.
Use linecache for a more elegant solution, which will be quite fast for reading many files, possible repeatedly.
Take @Alok's advice and use enumerate() for files which might be very large, and won't fit into memory. Note that using this method might slow because the file is read sequentially.
Link: http://stackoverflow.com/a/2081861

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment