我正在学习
Python.我有一个函数readwrite(filename,list). filename的类型为string. list是一个包含要在文件中重写的字符串的列表.
fname = 'hello.txt' readwrite('xx'+fname,datalist)
我面临的问题是,当我在函数定义中打印文件名参数值时,我得到hello.txt而不是xxHello.txt – 这是一个奇怪的事情,当我从commadline做同样的事情时我没想到,对于一个示例函数,它工作正常.
我想知道我在那里失踪了什么.
这是代码:
def readwrite(fileName,list): print 'arg file=',filename curdir = os.getcwd(); fullpath = os.path.join(curdir,filename); print('full path calculated as: '+fullpath); fileExist = os.path.exists(fullpath); if(fileExist): print 'file exists and opening in \'arw\'mode' fiel = open(fileName,'arw') # valid only if exists else: print "file doesnt exist; opening in \'w\'mode" fiel = open(fileName,'w') # if it doesnt exist,we cant open it for reading as nothing to read. for line in list: fiel.write('\n'+line) print 'position of new pointer = ',fiel.tell()
filename = 'put.txt' strList = ['hello','how','are','you'] readwrite(('xx'+filename),strList);
– fn def print’arg file =’中的第二行,filename打印hello.txt而不是xxHello.txt
这是我的困惑,为什么它表现得很好,或者我做错了什么.