关于第29课record任务的疑问
file_name_boy = 'boy_'+ str(count) +'.txt'file_name_girl = 'girl_' + str(count) + '.txt'
boy_file = open(file_name_boy, 'w')
girl_file = open(file_name_girl, 'w')
我想把文件存在指定文件目录下,怎么写代码啊,比如保存在文件夹内:'C:\\Users\\Desktop\\pyth
如下代码也只会生成一个叫做file_name_boy的文件
boy_file = open('C:\\Users\\\Desktop\\pyth\\file_name_boy','w')
本帖最后由 liuzhengyuan 于 2020-8-31 15:32 编辑
使用 f-string ?
boy_file = open(f'C:/Users/Desktop/pyth/{file_name_boy}','w') 本帖最后由 sunrise085 于 2020-8-31 15:33 编辑
写错啦
file_name_boy是一个变量,你把它写进字符串当然只能生成名为file_name_boy的文件啦
boy_file = open('C:\\Users\\Desktop\\pyth\\%s'%file_name_boy,'w')
页:
[1]