tell

Is there a way to go back when reading a file using seek and calls to next()?

爱⌒轻易说出口 提交于 2020-05-13 05:58:36
问题 I'm writing a Python script to read a file, and when I arrive at a section of the file, the final way to read those lines in the section depends on information that's given also in that section. So I found here that I could use something like 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() Yet, the structure of my current code is something like the

Is there a way to go back when reading a file using seek and calls to next()?

安稳与你 提交于 2020-05-13 05:57:09
问题 I'm writing a Python script to read a file, and when I arrive at a section of the file, the final way to read those lines in the section depends on information that's given also in that section. So I found here that I could use something like 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() Yet, the structure of my current code is something like the

Is there a way to go back when reading a file using seek and calls to next()?

流过昼夜 提交于 2020-05-13 05:57:05
问题 I'm writing a Python script to read a file, and when I arrive at a section of the file, the final way to read those lines in the section depends on information that's given also in that section. So I found here that I could use something like 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() Yet, the structure of my current code is something like the

f.seek() and f.tell() to read each line of text file

a 夏天 提交于 2019-12-19 07:29:22
问题 I want to open a file and read each line using f.seek() and f.tell() : test.txt: abc def ghi jkl My code is: 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 It reads the whole file. 回答1: ok, you may use this: f = open( ... ) f.seek(last_pos) line = f.readline() # no 's' at the end of `readline()` last_pos = f.tell() f.close()

Python file.tell gives wrong value location

蹲街弑〆低调 提交于 2019-12-18 17:11:36
问题 I am trying to extract a number of locations from an existing file using Python. This is my current code for extracting the locations: 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") This snippet is repeated a number of times (less the file open) with different search values, and seems to work: I get the correct messages, and the variables have

Finding position in file during iteration

杀马特。学长 韩版系。学妹 提交于 2019-12-10 16:24:47
问题 I am trying to use f.tell() in a normal text file during iteration: with open('test.txt') as f: for line in f: print(f.tell()) I get the following error: Traceback (most recent call last): File "<stdin>", line 3, in <module> OSError: telling position disabled by next() call Just to make sure, I checked that the same error occurs if I try skipping a line manually, discarding the iterator object (which is probably the file itself): with open('test.txt') as f: next(f) print(f.tell()) My end goal

tell application - string vs. string?

心不动则不痛 提交于 2019-12-09 23:42:33
问题 If I run: tell application "Maps" set miniaturized of windows to false end tell ...this works fine Yet, when I run: set applicationName to "Maps" tell application applicationName set miniaturized of windows to false end tell ...I get: Maps got an error: Can’t make |miniaturized| of every window into type reference. I also tried: tell application (applicationName as string) ... end tell ...but I get the same error. I'm new to Apple Script and not quite understanding the nuances between the two

tell application - string vs. string?

一个人想着一个人 提交于 2019-12-04 20:08:19
If I run: tell application "Maps" set miniaturized of windows to false end tell ...this works fine Yet, when I run: set applicationName to "Maps" tell application applicationName set miniaturized of windows to false end tell ...I get: Maps got an error: Can’t make |miniaturized| of every window into type reference. I also tried: tell application (applicationName as string) ... end tell ...but I get the same error. I'm new to Apple Script and not quite understanding the nuances between the two. The argument of tell application is required be a literal string (a constant) because the terminology

Python file.tell gives wrong value location

南楼画角 提交于 2019-11-30 15:32:34
I am trying to extract a number of locations from an existing file using Python. This is my current code for extracting the locations: 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") This snippet is repeated a number of times (less the file open) with different search values, and seems to work: I get the correct messages, and the variables have values. However, using the code below, the first write location is wrong, while subsequent write

Deleting a line from a huge file in Perl

安稳与你 提交于 2019-11-27 09:33:51
I have huge text file and first five lines of it reads as below : This is fist line This is second line This is third line This is fourth line This is fifth line Now, I want to write something at a random position of the third line of that file which will replace the characters in that line by the new string I am writing. I am able to achieve that with the below code : 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); However, I am not able to figure out with the same kind of