-1

I have a file that contains several Phone Number. Now I want to convert any line of this file to VCF file. So,first i defined e template model for VCF file that have a String "THISNUMBER" And i want to open file (thats have phone numbers) and replace thats lines to Template model (THISNUMBER)

i write this Python code :

template = """BEGIN:VCARD
VERSION:3.0
N:THISNUMBER;;;
FN:THISNUMBER
TEL;TYPE=CELL:THISNUM
END:VCARD"""

inputfile=open('D:/xxx/lst.txt','r')
counter=1
for thisnumber in inputfile:
    thisnumber=thisnumber.rstrip()
    output=template.replace('THISNUMBER',thisnumber)
    outputFile=('D:/xxx/vcfs/%05i.vcf' % counter,'w')
    outputFile.write(output)
    output.close
    print ("writing file %i") % counter
    counter +=1
    inputfile.close()

But I Give This ERROR :

Traceback (most recent call last):
 File "D:\xxx\a.py", line 16, in <module>
 outputFile.write(output)
 AttributeError: 'tuple' object has no attribute 'write'

2 Answers2

0

change

outputFile=('D:/xxx/vcfs/%05i.vcf' % counter,'w')

to

outputFile=open('D:/xxx/vcfs/%05i.vcf' % counter,'w')

Astrom
  • 767
  • 5
  • 20
0

I'll write a full fledged answer because I want to address your code style, if that's fine.

The problem is likely that you forgot to call open() on your outputFile. But let me introduce to you a nice way of handling files in Python. This way you don't even have to remember to call close(). It is all done with a context manager. The file is closed when the with statement exits.

template = """BEGIN:VCARD
VERSION:3.0
N:THISNUMBER;;;
FN:THISNUMBER
TEL;TYPE=CELL:THISNUM
END:VCARD"""

with open('D:/xxx/lst.txt', 'r') as inputfile:
    counter = 1
    for number in inputfile:
        number = number.rstrip()
        output = template.replace('THISNUMBER', number)
        with open('D:/xxx/vcfs/%05i.vcf' % counter, 'w') as outputFile:
            outputFile.write(output)

        print('writing file %i' % counter)
        counter += 1
Felix
  • 2,548
  • 19
  • 48