2010-08-31 51 views
8

有沒有人們在Python Interactive Startup scripts中輸入常用的時間符號?當我嘗試執行相關文件操作或import時,我使用win32模塊更改控制檯窗口的名稱,以幫助我知道自己在哪裏。你的Python交互式啓動腳本是什麼?

import sys 
import os 
import win32api 
__title_prefix = 'Python %i.%i.%i %s %s' % (sys.version_info[0:4] + 
              (sys.version_info[4] or "",)) 

def __my_chdir(path): 
    __os_chdir(path) 
    win32api.SetConsoleTitle(__title_prefix + " - " + os.getcwd()) 

# replace chdir func 
__os_chdir = os.chdir 
os.chdir = __my_chdir 

os.chdir(r'C:\Scripts') 
+0

雖然這個有着驚人的答案,我將我自己的啓動腳本,我已經投票決定關閉這爲是主要的意見根據。 – ArtOfWarfare 2015-02-24 20:20:57

回答

4
# Add auto-completion and a stored history file of commands to your Python 
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is 
# bound to the Esc key by default (you can change it - see readline docs). 
# 
# Store the file in ~/.pystartup, and set an environment variable to point 
# to it: "export PYTHONSTARTUP=/home/user/.pystartup" in bash. 
# 
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the 
# full path to your home directory. 

import atexit 
import os 
import readline 
import rlcompleter 

readline.parse_and_bind("tab: complete") 
historyPath = os.path.expanduser("~/.history.py") 

def save_history(historyPath=historyPath): 
    import readline 
    readline.write_history_file(historyPath) 

if os.path.exists(historyPath): 
    readline.read_history_file(historyPath) 

atexit.register(save_history) 
del os, atexit, readline, rlcompleter, save_history, historyPath 
+0

unix只能從http://docs.python.org/library/readline.html看起來像 – 2010-08-31 22:03:01

+3

@Nic什麼?!那裏有非UNIX系統? :-) – Anycorn 2010-08-31 22:06:26

+0

Psh,我大部分時間都在做嵌入式開發,所以使用* nix對我來說實際上是不可能的,因爲絕大多數編譯器,程序員和調試器都是Windows的目標。 – 2010-08-31 22:11:48

4

我也用see,對眼睛的替代Python的目錄更容易。

from see import see 

,而不是dir使用see的示例如下:

>>> k = {} 
>>> dir(k) 
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__','__doc__', 
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', 
'__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 
'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 
'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] 
>>> see(k) 
    []    in    <    <=    == 
    !=    >    >=    hash()   help() 
    iter()   len()   repr()   str()   .clear() 
    .copy()   .fromkeys()  .get()   .has_key()  .items() 
    .iteritems() .iterkeys()  .itervalues() .keys()   .pop() 
    .popitem()  .setdefault() .update()  .values() 
相關問題