Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

first and last character "p" is deleted when writing file on filesystem using python

I am trying tot write a file and remove the .pdf extension and replacing it with .xml extension.
But now, if the filename starts or ends with the letter p, it gets removed.

tree = ET.ElementTree(root).write(base_dir+str(file_name).strip(‘.pdf’)+".xml", method=’xml’, encoding=’UTF-8′, xml_declaration=True)

So filename help.pdf will be writen as hel.xml
or press.pdf will be writen as ress.xml

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if i remove .strip(‘.pdf’) from the line it doesn’t remove the p, but ofcourse it will write .pdf.xml

i hope someone can help me.

Greets Dyllan

>Solution :

strip removes any leading or trailing sequence composed of any of the characters included in the specified string. You want to use e.g. replace(".pdf", "") instead. Or better, do replace(".pdf", ".xml") and remove that +".xml".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading