如何在 mac OSX 10.8.2 上用 python 打开文件

问题描述 投票:0回答:3

我正在 eclipse 上编写 python 代码,想要打开“下载”文件夹中存在的文件。我使用的是 MAC OSX 10.8.2。我尝试过

f=os.path.expanduser("~/Downloads/DeletingDocs.txt")
还有

ss=subprocess.Popen("~/Downloads/DeletingDocs.txt",shell=True)
ss.communicate()

我基本上想在子进程中打开一个文件,以监听打开的文件中的更改。但是,在任何一种情况下该文件都没有打开。

python macos file subprocess popen
3个回答
4
投票
from os.path import abspath, expanduser
filepath = abspath(expanduser("~/") + '/Downloads/DeletingDocs.txt')
print('Opening file', filepath)
with open(filepath, 'r') as fh:
    print(fh.read())

注意 OSX 文件处理,根据文件类型的不同,IO 略有不同。 例如,在 Windows 下被视为“纯文本文件”的

.txt
文件实际上是 OSX 下的压缩数据流,因为 OSX 试图“智能”存储空间。

这可能会毁掉你的一天,除非你知道它(去过那里,头痛......继续前进)

例如,当在 OSX 中双击

.txt
文件时,通常会弹出文本编辑器,它的作用是调用
os.open()
,而不是在较低级别访问它,这让 OSX 中间层可以执行
disk-area|decompression pipe|file-handle -> Texteditor
操作但是如果您在较低级别访问文件对象,您最终将打开存储文件的磁盘区域,如果您打印数据,您将得到垃圾,因为它不是您期望的数据。

所以尝试使用:

import os
fd = os.open( "foo.txt", os.O_RDONLY )
print(os.read(fd, 1024))
os.close( fd )

摆弄旗帜。 老实说,我不记得两者中的哪一个从磁盘按原样打开文件(

open()
os.open()
),但其中一个使您的数据看起来像垃圾,有时您只是获得指向减压管道的指针(给出你喜欢 4 字节的数据,即使文本文件很大)。

如果它正在跟踪/捕获您想要的文件的更新

from time import ctime
from os.path import getmtime, expanduser, abspath
from os import walk

for root, dirs, files in walk(expanduser('~/')):
    for fname in files:
        modtime = ctime(getmtime(abspath(root + '/' + fname)))
        print('File',fname,'was last modified at',modtime)

如果时间与您上次检查的时间不同,那么就用它做一些很酷的事情。 例如,您有以下供 Python 使用的库:

还有更多,所以不要将打开外部应用程序作为您的第一个解决方案,而是尝试通过 Python 打开它们并根据您的喜好进行修改,并且仅作为最后的手段(即使如此)通过 Popen 打开外部应用程序。

但是既然你要求它(有点......呃),这里有一个 Popen 方法

from subprocess import Popen, PIPE, STDOUT
from os.path import abspath, expanduser
from time import sleep

run = Popen('open -t ' + abspath(expanduser('~/') + '/example.txt'), shell=True, stdout=PIPE, stdin=PIPE, stderr=STDOUT)

##== Here's an example where you could interact with the process:
##== run.stdin.write('Hey you!\n')
##== run.stdin.flush()

while run.poll() == None:
    sleep(1)

过度解释你的工作:

这将在每次更改时打印文件内容。

with open('test.txt', 'r') as fh:
    import time
    while 1:
            new_data = fh.read()
            if len(new_data) > 0:
                fh.seek(0)
                print(fh.read())
            time.sleep(5)

它是如何工作的: 常规文件打开器

with open() as fh
将打开文件并将其作为句柄放置在
fh
中,一旦您不带任何参数调用
.read()
,它将获取文件的全部内容。 这反过来不会关闭文件,它只是将“读取”指针放在文件的后面(为了方便起见,我们可以说在位置 50)。

现在您的指针位于文件末尾的第 50 个字符处。 无论您在文件中的何处写入内容,都会将更多数据放入其中,因此下一个

.read()
将从位置 50+ 获取数据,使
.read()
不为空,因此我们通过发出以下命令将“读取”指针放回位置 0
.seek(0)
然后我们打印所有数据。

将其与

os.path.getmtime()
结合起来,以罚款任何反向更改或 1:1 比例更改(替换字符拼写错误等)。


2
投票

我很犹豫是否要回答,因为这个问题被重复发布并且措辞混乱,但是...如果您想使用默认编辑器在 OSX 中“打开”文件,则将

open
命令添加到您的子进程中。这对我有用:

subprocess.Popen("open myfile.txt",shell=True)

0
投票

这很可能是权限问题,如果您在 Python 解释器中尝试代码,那么当您调用 subprocess.Popen 时,您可能会收到来自 shell 的“权限被拒绝”错误。 如果是这种情况,那么您需要将文件设置为至少 700(默认情况下可能是 644),并且您可能需要 744。

在 Python 解释器中尝试代码并检查是否有“权限被拒绝”错误,如果您看到该错误,则在 shell 中执行此操作:

chmod 744 ~/Downloads/DeletingDocs.txt

然后运行脚本。 要在 Python 中完成这一切,您可以使用 os.system:

import os
import subprocess

filename = "~/Downloads/DeletingDocs.txt"
os.system("chmod 744 "+filename)
ss=subprocess.Popen(filename, shell=True)
ss.communicate()

它在 Windows 中“正常工作”的原因是 Windows 不支持文件权限类型(读、写和执行),就像 *nix 系统(例如 Linux、BSD、OS X 等)那样。

© www.soinside.com 2019 - 2024. All rights reserved.