File handling problem.

SUBHABRATA BANERJEE subhakolkata1234 at gmail.com
Sun May 3 12:51:13 EDT 2009


Dear Group,



I am working on a code like the following:



from decimal import*

#SAMPLE TEST PROGRAM FOR FILE

def sample_file_test(n):

    #FILE FOR STORING PROBABILITY VALUES

    open_file=open("/python26/Newfile1.txt","r+")

    #OPENING OF ENGLISH CORPUS

    open_corp_eng=open("/python26/TOTALENGLISHCORPUS1.txt","r")

    #READING THE ENGLISH CORPUS

    corp_read=open_corp_eng.read()

    #CONVERTING THE CORPUS FILE IN WORDS

    corp_word=corp_read.split()

    #EXTRACTING WORDS FROM CORPUS FILE OF WORDS

    for word in corp_word:

        #COUNTING THE WORD

        count1=corp_word.count(word)

        #COUNTING TOTAL NUMBER OF WORDS

        count2=len(corp_word)

        #COUNTING PROBABILITY OF WORD

        count1_dec=Decimal(count1)

        count2_dec=Decimal(count2)

        getcontext().prec = 6

        prob_count=count1_dec/count2_dec

        print prob_count

        string_of_prob_count=str(prob_count)

        file_input_val=open_file.write(string_of_prob_count)

        open_file.close()



The problems I am getting:

(i)                  The probability values are not being stored properly in
file.

(ii)                “Newfile1.txt” is storing not the series of values but
an arbitrary value from series 0.00000143096

(iii)               As I was testing it again it gave me another error

Traceback (most recent call last):

  File "<pyshell#2>", line 1, in <module>

    sample_file_test(1)

  File "C:\Python26\testprogramforfiles1.py", line 25, in sample_file_test

    file_input_val=open_file.write(string_of_prob_count)

ValueError: I/O operation on closed file



If you can kindly let me know what is the wrong I am doing here.



As I took out the code pasted it in MS-Word before posting, there may be
slight indentation problem.



Best Regards,

Subhabrata.




On Sat, May 2, 2009 at 2:16 PM, Pascal Chambon <chambon.pascal at wanadoo.fr>wrote:

> subhakolkata1234 at gmail.com a écrit :
>
>>  Dear Group,
>>
>> I am using Python2.6 and has created a file where I like to write some
>> statistical values I am generating. The statistical values are
>> generating in a nice way, but as I am going to write it, it is not
>> taking it, the file is opening or closing properly but the values are
>> not getting stored. It is picking up arbitrary values from the
>> generated set of values and storing it. Is there any solution for it?
>>
>> Best Regards,
>> SBanerjee.
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>>
>>
> Hello
>
> Could you post excerpt of your file-handling code ?
> It might be a buffering problem (although when the file closes, I think
> buffers get flushed), else it's really weird...
>
> Regards,
> pascal
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090503/6b66218d/attachment.html>


More information about the Python-list mailing list