[Tutor] Checking for file completion.

Dave Angel d at davea.name
Tue Feb 21 22:56:09 CET 2012


On 02/21/2012 01:42 PM, Tony Pelletier wrote:
>> Please post your message again, as a text message rather than an html one.
>>   Reading non-trivial python code that's lost all its indentation is
>> impossible.  You've done it before, but that case was simple enough to not
>> matter much.
>>
>>
>>
>> --
>>
>> DaveA
>>
>
> Sorry about that.  I actually thought i was... my bad...
>
> Hi,
>
> I'm struggling with what I think seems to be a problem.  I've created
> a program that does numerous SOAP calls.  In short, I create a report
> on a report server, pull that file down then parse that file that's
> been written locally for data to make more SOAP calls.  My problem is
> I'm grabbing the filename then losing it somewhere along the way.  The
> only thing I can think of is that it's still open/being written and
> causing me a problem.
>
> Main starts like such.
>
> def main():
> 	service = Service()
> 	cleanup()
> 	reportId = createReport(service)
> 	#time.sleep(3)
> 	filename = getReport(service,reportId)
> 	getEventAttachments(service, filename)
>
> Here are the results from a test run.
>
> Deleting "files" directory...
> "files" directory successfully created...
> Report in Processing state...
> WeeklyDeliveriesReport_2012-02-21.csv
> WeeklyDeliveriesReport_2012-02-21.csv report succesfully written out...
> None
> File still being written out...
>
>
> getReport() returns a filename and you actually see it twice up in the results.
>
> def getReport(service, reportId):
> 	reportIds = service.client.factory.create('ArrayOfstring')
> 	reportIds.string.append(reportId)
>
> 	try:
> 		result = service.client.service.ReportQueryById(reportIds, 'True')
> 		if result.Report[0].ReportStatus == 'Processing':
> 			print 'Report in Processing state...'
> 			time.sleep(3)
> 			getReport(service,reportId)

Dd you intend to call yourself recursively here?  I don't understand all 
your logic here, but if you really did, I'd at least expect you to save 
the return value from the recursive call.  This is also the first place 
you're missing a return statement.

Is it possible you're trying to use recursion to substitute for a while 
loop?  Generally a bad idea.

>
> 		else:
>
> 			filename =  result.Report[0].ReportFileArgs.ReportFileArg[0].UserFileName
> 			print filename
> 			encodedfile = result.Report[0].ReportFileArgs.ReportFileArg[0].EncodedValue
>
> 			encodedstring = encodedfile.encode('utf-8')
> 			str_list = []
> 			for line in encodedstring:
> 				line = line.rstrip()
> 				str_list.append(line)
> 			string = ''.join(str_list)
> 			data = base64.b64decode(string)
> 			outfile = open(filename, 'w')
> 			outfile.write(data)
> 			outfile.close()
> 			shutil.copyfile(filename, os.path.join('files', filename))
> 			print filename + ' report succesfully written out...'
> 			logging.info(filename + ' report succesfully written out...')
> 			return filename
> 	except suds.WebFault as e:
> 		print e.fault.detail

Missing a return statement in the case of an exception.

>
> then from main, I'm using filename to then make the call to
> getEventAttachments and passing in filename.  The None you see is a
> result of my "print filename" statement.
>

Clearly this is Python 2.x code.  print is a statement and has no return 
value (result).  You don't state that this is the whole code, but if so, 
then you're missing return statements from several exit points in the 
function.  It's probably that missing return statement that gives you 
the None.


> Now, if I uncomment that time.sleep(3), it runs flawlessly which I
> think tells me that the file is still being written to.  I *think*...
>
> Two questions.
> 1.  Is that not it and if so, am I missing something obvious?
> 2.  If it is it, is there a way to check to see if the file I'm trying
> to read from is done being written to?
>
> Thanks
>


-- 

DaveA


More information about the Tutor mailing list