为什么Python';s读取历史文件?

为什么Python';s读取历史文件?,python,readline,Python,Readline,所以。我拥有名为~/.osc_history的文件,如下所示: $ w 17:53 up 3:15, 5 users, load averages: 1.30 1.17 1.10 USER TTY FROM LOGIN@ IDLE WHAT kyma console - 14:39 3:14 - kyma s001 - 17:20 - w $

所以。我拥有名为~/.osc_history的文件,如下所示:

$ w
17:53  up  3:15, 5 users, load averages: 1.30 1.17 1.10
USER     TTY      FROM              LOGIN@  IDLE WHAT
kyma     console  -                14:39    3:14 -
kyma     s001     -                17:20       - w

$ ls -l ~/.osc_history 
-rw-r--r--  1 kyma  staff  13 Mar  6 17:41 /Users/kyma/.osc_history

$ ls -lO ~/.osc_history 
-rw-r--r--  1 kyma  staff  - 13 Mar  6 17:41 /Users/kyma/.osc_history
文件中的头是正确的:

$ cat ~/.osc_history 
_HiStOrY_V2_
在交互式提示中,以下代码运行良好:

$ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os.path
>>> import readline
>>> histfile = os.path.join(os.path.expanduser("~"), ".osc_history")
>>> histfile
'/Users/kyma/.osc_history'
>>> readline.read_history_file(histfile)
>>> ^D
然而,当我尝试运行以下程序时,我称之为“yeuch.py”

……我明白了:

$ python yeuch.py
Traceback (most recent call last):
  File "yeuch.py", line 14, in <module>
    readline.read_history_file(histfile) # Read history from previous sessions
IOError: [Errno 1] Operation not permitted
$python yeuch.py
回溯(最近一次呼叫最后一次):
文件“yeuch.py”,第14行,在
readline.read_history_file(histfile)#从以前的会话中读取历史
IOError:[Errno 1]不允许执行操作

到底发生了什么事?(我希望我没有多次忽略一些愚蠢的打字错误…

(顺便说一句,没有其他任何东西正在运行或.osc_历史打开。我已经多次删除并重新创建了它,以查看是否有任何不可靠的地方。但不,它似乎是正确的。)@Posh_Pumpkin这不仅仅是合法的:我最初有另一条错误消息,按照中的说明解决了这一问题。您可以使用不同的文件名写入另一个文件吗?如果是,则表示您没有写入的权限。osc_history我在我的机器上尝试了您所拥有的功能,效果很好。我可以在OS X El Capitan上复制此功能,但我必须使用
/usr/bin/python
(我安装了brew
/usr/local/bin/python
)。我无法诊断dtruss的问题,因为OSX不允许我在受限二进制文件上使用它。
$ python yeuch.py
Traceback (most recent call last):
  File "yeuch.py", line 14, in <module>
    readline.read_history_file(histfile) # Read history from previous sessions
IOError: [Errno 1] Operation not permitted