Reputation: 4139
Trying to read some lines of a file (as done here) and having problem where even though I can count the lines of the file, the readline()
method for the file appears to return nothing.
Code snippet below (I have additional code showing how I am initially reading in these files to the system (via FTP download with ftplib
) in case that is relevant to the problem (since I don't really know what could be causing this weirdness)). The with open(localname)...
line near the bottom is where I start trying to read the lines.
# connect to ftp location
ftp = ftplib.FTP(MY_IP)
ftp.login(CREDS["source"]["ftp_creds"]["username"], CREDS["source"]["ftp_creds"]["password"])
print(ftp.pwd())
print(ftp.dir())
ftp.cwd(CONF["reporting_configs"]["from"]["share_folder_path"])
print(ftp.pwd())
print(ftp.dir())
# download all files from ftp location
print(f"Retrieving files from {CONF['reporting_configs']['from']['share_folder_path']}...")
files = ftp.nlst()
files = [f for f in files if f not in ['.', '..']]
pp.pprint(files)
for f in files:
# timestamp = int(time.time())
# basename = os.path.split(f)[0]
localname = os.path.join(CONF["stages_base_dir"], CONF["stages"]["imported"], f"{f}")
fd = open(localname, 'wb')
ftp.retrbinary(f"RETR {f}", callback=fd.write)
fd.close()
SAMPLE_LINES = 5
with open(localname) as newfile:
print(f"Sampling {newfile.name}")
for i, _ in enumerate(newfile):
pass
lines = i+1
print(lines)
for i in range(min(SAMPLE_LINES, lines)):
l = newfile.readline()
print(l)
The output looks like...
<after some other output>
.
.
.
Retrieving files from /test_source...
['TEST001.csv', 'TEST002.csv']
Sampling /path/to/stages/imported/TEST001.csv
5
Sampling /path/to/stages/imported/TEST002.csv
5
Notice that it is able to recognize that there are more than 0 lines in each file, but printing the readline()
shows nothing, yet viewing the textfile on my system I can see that there is not nothing.
Anyone know what could be going on here? Anything more I can do to debug?
Upvotes: 0
Views: 487
Reputation: 446
After testing on my machine if you add newfile.seek(0)
to reset the pointer back to the start of the file, it works.
SAMPLE_LINES = 5
with open("test.txt") as newfile:
print(f"Sampling {newfile.name}")
for i, _ in enumerate(newfile):
pass
lines = i+1
print(lines)
newfile.seek(0)
for i in range(min(SAMPLE_LINES, lines)):
l = newfile.readline()
print(l)
The only reason I can think that this could fix it is that when you run the for loop with the enumerate(newfile)
that function itself is using the newfile.readline() function and is therefore moving the pointer.
Upvotes: 2