在Mac上使用Python打开.pages文件 [英] Open a .pages file on Mac with Python
本文介绍了在Mac上使用Python打开.pages文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要打开这样的页面文档:
I want to open a pages doc like this:
directory = "/Path/to/file/"
with open(directory+"test.pages") as file:
data = f.readlines()
for line in data:
words = line.split()
print words
然后我收到此错误:
IOError: [Errno 21] Is a directory: '/path/to/file/test.pages'
为什么这是目录? 那我怎么打开呢?
Why is this a directory? And how do I open it then?
推荐答案
'/path/to/file/test.pages'
是文件系统上的目录,因此无法在Python中打开.您的操作系统正在捆绑该目录中的多个文件,并且可能将其显示为单个软件包.您可以想像地遍历目录并获取内容:
'/path/to/file/test.pages'
is a directory on your file system, therefore it cannot be opened in Python. Your OS is bundling several files in that directory, and perhaps presenting it as a single package. You could conceivably walk the directory and get the contents:
for root, dirs, files in os.walk('/path/to/file/test.pages'):
for file in files:
print os.path.join(root, file)
但是打开文件并尝试读取其内容很可能是徒劳的.
But opening the files and trying to read in their contents it likely to be fruitless.
我将告诉您如何尝试查找任何纯文本:
I'll show you how to attempt to find any plain-text:
import re
# use a pattern that matches for any letter A-Z, upper and lower, 0-9, and _
pattern = re.compile(r'.*\w+.*')
for root, dirs, files in os.walk('/path/to/file/test.pages'):
for file in files:
# open each file with the context manager so it's automatically closed
# regardless if there's an error. Use the Universal Newlines (U) flag too
# as a best practice (Unix, Linux, and MS have different newlines).
with open(os.path.join(root, file), 'rU') as f:
for line in f:
if re.match(pattern, line):
print line
这篇关于在Mac上使用Python打开.pages文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文