0

I have wrirtten a python script which does the following:

1)It reads a journal file(.jrn) within a zip file from a particular path and writes the same to a text file on another path.

2) The text file is read by another python script(say x.py) to do some particular operations.

The problem im facing is, the text file which is written, though its contents are appearing same as in the journal file(which is read from zipfile), when it is read by another python script say x.py, some special characters are read, causing the script to fail.

When the x.py script reads the original jrn file(in zip file), it is getting processed successfully..

I use python 2.4 version, so i cant be using extract() function of Zipfile library.

I just want the contents of text file which is written,to be exactly same as the contents of jrn file which is read from zip. pls help.

Code:

fout = zipfile.ZipFile(os.path.join(Out_path_Afp,Out_Path_Afp_File),'r')
files = fout.namelist()
dir = filter(lambda x:os.path.splitext(string.lower(x))[1]=='.jrn',files)
out_zip_files_pdf = re.compile('WW_'+Input_file_name+'_Restsoe_toload_prod.jrn')
pdf_jrn_list = filter(lambda x:out_zip_files_pdf.match(x),files)
for pdf_jrn_ls in pdf_jrn_list:
    pdf_jrn = pdf_jrn_ls    
print pdf_jrn
data_jrn_pdf = fout.read(pdf_jrn)
txt_outpath = "e:\\senthil\\log_recon\\jrn\\"
txt_outfile_pdf = time.strftime("%Y%m%d_%H%M",LocalTime)+'WW_'+Input_file_name+'_Restsoe_toload_prod.txt'

Output_Ptr_pdf = open(txt_outpath + txt_outfile_pdf,'w')
Output_Ptr_pdf.write(data_jrn_pdf)
Output_Ptr_pdf.close()
wRAR
  • 25,009
  • 4
  • 84
  • 97
user1240863
  • 41
  • 1
  • 2
  • 4

1 Answers1

0

If you are doing a straight copy, there's no need to drop down to pulling the data out of the file yourself. Just unzip and How do I copy a file in python?

Though it looks like you may not be doing a straight copy. Is the jrn file text encoded? And are you trying to write to a PDF? Your code suggests so, but your description does not.

Community
  • 1
  • 1
Silas Ray
  • 25,682
  • 5
  • 48
  • 63
  • Hi.. Im trying to write to a txt file and the jrn file is not encoded. – user1240863 Mar 01 '12 at 17:54
  • Hi.. Im trying to write to a txt file and the jrn file is not encoded.In the plain text, the contents of the text file is exactly same the jrn file.. But internally some special characters are getting inserted.. – user1240863 Mar 01 '12 at 17:57
  • So jrn is straight ASCII or UTF-8, and you are trying to just copy the file in the same text encoding, with the same content, correct? If so, unziping and using the above solutions in that thread should do the job just fine. – Silas Ray Mar 01 '12 at 17:57
  • Oh, I think I see. You are trying to compile the contents of a list of files in to a single file. I'm not sure what you think that for loop is doing though; all you end up doing with that is working with the last pdf_jrn in the list, which could much more easily be accomplished with just pdf_jrn = pdf_jrn_list[-1]. I think you mean to have more of that code in the loop. – Silas Ray Mar 01 '12 at 18:03
  • yeah.. your assumption is correct. But im working on python 2.4 version,where extract() doesnt work.. python 2.4 is the version used in business,which i cant change,as of now? Is there any other way, other than unzipping? – user1240863 Mar 01 '12 at 18:23
  • let me try with pdf_jrn = pdf_jrn_list[-1] – user1240863 Mar 01 '12 at 18:25
  • http://stackoverflow.com/questions/7806563/how-to-unzip-a-zip-file-with-python-2-4 – Silas Ray Mar 01 '12 at 18:31