目前我有这段代码。
class FileOperations:
def open_files(sys):
sys.DM = open('DM_FILE.txt','a')
sys.TM = open('TM_FILE.txt','a')
sys.AP = open('AP_FILE.txt','a')
def write_header_to_files(auto):
global x,y,z
x= 'PRINT_PUB_CODE,PRINT_ACCT_NUM,HOME PHONE..'
y= 'PRINT_PUB_CODE,PRINT_ACCT_NUM,OFFICE PHONE,..'
z= 'PRINT_PUB_CODE,PRINT_ACCT_NUM,CCELLPHONE,..'
auto.DM_FILE.write('x')
auto.TM_FILE.write('y')
auto.AP_FILE.write('z')
我使用
在diff脚本中调用该类filehandle = FileOperations()
filehandle.open_files()
filehandle.write_header_to_files()
我不确定如何在if循环中调用单个文件然后再写入。
在帮助之后,这就是我所做的。
class FileOperations:
def open_files(self):
self.file= {
'DM':open('DM_FILE.txt','a'),
'TM':open('TM_FILE.txt','a'),
'AM':open('AP_FILE.txt','a'),
'DM1':open('DM_EMAIL_FILE.txt','a'),
'TM1':open('TM_EMAIL_FILE.txt','a'),
'AP1':open('AP_EMAIL_FILE.txt','a')
}
def write_header_to_files(self):
global x,y,z
x= 'PRINT_PUB_CODE,PRINT_ACCT_NUM,..
y= 'PRINT_PUB_CODE,CO_NAME,PREFIX,..
z= 'PRINT_PUB_CODE,PREFIX,FIRST_NAME,
self.write={
'MD' : DM_FILE.write(x),
'MT' : TM_FILE.write(y),
'PA' : AP_FILE.write(z),
'MD1' : DM_EMAIL_FILE.write(x),
'MT1' : TM_EMAIL_FILE.write(y),
'PA1' : AP_EMAIL_FILE.write(z)
}
如果有人能说这段代码对我想要的东西是正确的,那么真的很感激。我怎样才能从另一个脚本中调用这个函数。
答案 0 :(得分:1)
class FileOperations:
def __init__(self):
self.files = {
'DM' : open('DM_FILE.txt','a'),
'TM' : open('TM_FILE.txt','a'),
'AP' : open('AP_FILE.txt','a')
}
def write_header_to_file(self, file_id):
global x
self.files[file_id].write(x)
filehandle = FileOperations()
filehandle.write_header_to_file('DM')
filehandle.write_header_to_file('TM')