-2

This is my

 import os

  filenames= os.listdir (".")  
file = open("XML.txt", "w")
result = []


for filename in filenames:
    result = "<capltestcase name =\""+filename+"\"\n"
    file.write(result)
    result = "title =  \""+filename+"\"\n"
    file.write(result)
    result = "/>\n"
    file.write(result)
file.close()

My Question /help needed I want to add standard text "" to the txt generated, but i cant add it, it says sytax errors can somebody help with code please.

2) how can i just copy foldernames from directory instead of file names , since with my code , it copies all file names in into txt.

Thank you frnds ..

file.write("\\")

patha
  • 1

2 Answers2

0

use the escape () to write special characters

print("\\<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\\")
WorkaroundNewbie
  • 1,063
  • 9
  • 16
0

Rather than escaping all those double-quotes, why not embed your string inside single quotes instead? In Python (unlike many other languages) there is no difference between using single or double quotes, provided they are balanced (the same at each end).

If you need the backslashes in the text then use a raw string

file.write(r'"\<?xml version="1.0" encoding="iso-8859-1"?>\"')

That will preserve all the double-quotes and the back-slashes.

Community
  • 1
  • 1
cdarke
  • 42,728
  • 8
  • 80
  • 84