标签: tell

使用Applescript通过特定的完整路径"告诉"特定的应用程序

我想告诉应用程序"xyz",但通过指定应用程序的完整路径.这是因为系统上的应用程序可能存在不同版本,但名称相同.如果这可能吗?

applescript tell

16
推荐指数
2
解决办法
8404
查看次数

AppleScript:在tell语句中调用处理程序

每次运行此脚本时都会出现此错误:系统事件出错:"Test123"无法理解通知消息.

码:

--more code...
tell application "System Events"
    if some_system_events_property then
         my notify of "Test123" thru "Test"
    end if
end tell
--more code...
to notify of message thru level
    display dialog message with titel level
end notify
Run Code Online (Sandbox Code Playgroud)

我试图替换

my notify of "Test123" thru "Test"
Run Code Online (Sandbox Code Playgroud)

以下,没有任何成功:

notify of "Test123" thru "Test" of me
(notify of "Test123" thru "Test") of me
Run Code Online (Sandbox Code Playgroud)

applescript handler tell

6
推荐指数
2
解决办法
4383
查看次数

f.seek()和f.tell()读取文本文件的每一行

我想打开一个文件并使用f.seek()和读取每一行f.tell():

的test.txt:

abc
def
ghi
jkl
Run Code Online (Sandbox Code Playgroud)

我的代码是:

f = open('test.txt', 'r')
last_pos = f.tell()  # get to know the current position in the file
last_pos = last_pos + 1
f.seek(last_pos)  # to change the current position in a file
text= f.readlines(last_pos)
print text
Run Code Online (Sandbox Code Playgroud)

它读取整个文件.

python file-io seek tell

6
推荐指数
1
解决办法
5万
查看次数

可以在没有明显启动应用程序的情况下执行AppleScript"tell"调用吗?

我有一个邮件规则设置来启动以下AppleScript:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        tell application "Mail"

            -- do stuff, including...
            CheckAddressBook(theName, theAddress)

        end tell
    end perform mail action with messages
end using terms from

on CheckAddressBook(theName, theAddress)
    tell application "Address Book"
        -- do stuff
    end tell
end CheckAddressBook

每当执行此邮件规则时,它都会启动通讯簿.它没有激活,但突然出现在我的桌面上.我的问题是,是否可以告诉块以静默方式启动应用程序,并在完成后退出?

applescript tell

5
推荐指数
1
解决办法
1362
查看次数

在迭代期间查找文件中的位置

我试图f.tell()在迭代期间在普通文本文件中使用:

with open('test.txt') as f:
    for line in f:
        print(f.tell())
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
OSError: telling position disabled by next() call
Run Code Online (Sandbox Code Playgroud)

为了确保这一点,我检查了如果我尝试手动跳过一行并丢弃迭代器对象(可能是文件本身),是否会发生相同的错误:

with open('test.txt') as f:
    next(f)
    print(f.tell())
Run Code Online (Sandbox Code Playgroud)

我的最终目标是找到文件中第一行的长度(以字节为单位),无论平台如何,因此以下工作正常:

with open('test.txt') as f:
    f.readline()
    print(f.tell())
Run Code Online (Sandbox Code Playgroud)

我很好奇为什么tell在迭代过程中禁用了 using 。我可以理解为什么seek会被禁用,因为大多数迭代器不喜欢并发修改,但为什么呢tell?是否tell执行一些影响迭代器或类似操作的状态更改?

我可能应该提到我正在 Anaconda 环境中运行 Python 3.6.2。我在 Arch Linux 和 Red Hat 7.5 上都观察到了这种行为。

更新

这个问题似乎以不同的形式出现在 Python 2.7 中:file.tell() inconsistency。我想知道缓冲优化引起的不一致是否是tellPython 3中完全禁用的原因。

这实际上提出了一个更深层次的问题,这就是为什么 …

python file python-3.x tell

5
推荐指数
0
解决办法
855
查看次数

Python file.tell给出了错误的值位置

我试图使用Python从现有文件中提取许多位置.这是我当前提取位置的代码:

    self.fh = open( fileName , "r+")
    p = re.compile('regGen regPorSnip begin')
    for line in self.fh :
        if ( p.search(line) ):
            self.porSnipStartFPtr = self.fh.tell()
            sys.stdout.write("found regPorSnip")
Run Code Online (Sandbox Code Playgroud)

这个代码段重复了很多次(少了文件打开),具有不同的搜索值,似乎有效:我得到了正确的消息,变量有值.

但是,使用下面的代码,第一个写入位置是错误的,而后续写入位置是正确的:

    self.fh.seek(self.rstSnipStartFPtr,0)
    self.fh.write(str);
    sys.stdout.write("writing %s" % str )
    self.rstSnipStartFPtr = self.fh.tell()
Run Code Online (Sandbox Code Playgroud)

我已经读过,由于Python倾向于"提前读取",传递某些read/ readline选项fh会导致错误的判断值.我看到避免这种情况的一个建议是读取整个文件并重写它,这在我的应用程序中不是一个非常有吸引力的解决方案.

如果我将第一个代码段更改为:

  for line in self.fh.read() :
        if ( p.search(line) ):
            self.porSnipStartFPtr = self.fh.tell()
            sys.stdout.write("found regPorSnip")
Run Code Online (Sandbox Code Playgroud)

然后它似乎self.fh.read()只返回字符而不是整行.搜索从不匹配.这似乎也适用于此self.fh.readline().

我的结论是,fh.tell在写操作后查询时只返回有效的文件位置.

有没有办法在阅读/搜索时提取准确的文件位置?

谢谢.

python seek tell

4
推荐指数
1
解决办法
3089
查看次数

有没有办法在使用搜索和调用next()读取文件时返回?

我正在编写一个python脚本来读取文件,当我到达文件的某个部分时,在该部分中读取这些行的最终方法取决于该部分中给出的信息.所以我在这里发现我可以使用类似的东西

fp = open('myfile')
last_pos = fp.tell()
line = fp.readline()
while line != '':
  if line == 'SPECIAL':
  fp.seek(last_pos)
  other_function(fp)
  break
last_pos = fp.tell()
line = fp.readline()
Run Code Online (Sandbox Code Playgroud)

然而,我当前代码的结构如下所示:

fh = open(filename)

# get generator function and attach None at the end to stop iteration
items = itertools.chain(((lino,line) for lino, line in enumerate(fh, start=1)), (None,))
item = True

  lino, line = next(items)

  # handle special section
  if line.startswith['SPECIAL']:

    start = fh.tell()

    for i in range(specialLines):
      lino, eline = next(items) …
Run Code Online (Sandbox Code Playgroud)

python next seek python-3.x tell

4
推荐指数
1
解决办法
3189
查看次数

从Perl中的大文件中删除一行

我有一个巨大的文本文件,前五行如下:

This is fist line
This is second line
This is third line
This is fourth line
This is fifth line
Run Code Online (Sandbox Code Playgroud)

现在,我想在该文件的第三行的随机位置写一些东西,它将用我正在编写的新字符串替换该行中的字符.我能用以下代码实现这一点:

use strict;
use warnings;

my @pos = (0);
open my $fh, "+<", "text.txt";

while(<$fh) {
    push @pos, tell($fh);
}

seek $fh , $pos[2]+1, 0;
print $fh "HELLO";

close($fh);
Run Code Online (Sandbox Code Playgroud)

但是,我无法用同样的方法弄清楚如何从该文件中删除整个第三行,以便文本如下所示:

This is fist line
This is second line
This is fourth line
This is fifth line
Run Code Online (Sandbox Code Playgroud)

我不想将整个文件读入数组,也不想使用Tie :: File.是否有可能使用搜索和告诉来实现我的要求?解决方案将非常有用.

perl file seek tell

4
推荐指数
1
解决办法
676
查看次数

使用 zipfile 的 Python 错误:'list' 对象没有属性 'tell'

我正在尝试使用以下代码在目录中仅压缩 *.csv 文件:

allFiles = os.listdir( dirName + apt + '/' )
csvList  = [i for i in allFiles if i.endswith('.csv')]
zf       = zipfile.ZipFile([ dirName + apt + '.zip' ], mode='w')
for f in csvList:
    a    = dirName + apt + '/' + f
    zf.write( a )
#all the elements of a are strings
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Traceback (most recent call last):
File "<ipython-input-43-ebf4dc807b56>", line 1, in <module>
zf.write(a)
File "C:\Users\blevy\MCR\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\zipfile.py", line 1347, in write
zinfo.header_offset = self.fp.tell()    # Start of header …
Run Code Online (Sandbox Code Playgroud)

python zip list attributeerror tell

2
推荐指数
1
解决办法
4036
查看次数

标签 统计

tell ×9

python ×5

seek ×4

applescript ×3

file ×2

python-3.x ×2

attributeerror ×1

file-io ×1

handler ×1

list ×1

next ×1

perl ×1

zip ×1