file_name_1='Chapter10\cats.txt'
file_name_2='Chapter10\dogs.txt'
with open(file_name_1,'r') as file_object:
title=file_object.name
contents=file_object.read()
title=file_object.
petsname=contents.split()
print(petsname)
number=len(petsname)
strname='There are '+str(number)+title+' whose name are:'
for name in petsname:
strname+=(name+',')
print(strname[:-1]+'.')
代码如上,两个文件名分别是"cats"和“dogs”.如果用title=file_object.name只能输出相对路径即“Chapter10\cats.txt”。有没有什么办法只输出cats呢?
谢谢!