我安装的是Python3.6.4
用pycharm编写如下代码,本来是想在F:\python program\exercise\20190706文件下面新建txt和word文件,运行的结果却是在F:\python program文件下创建了txt和word文件,不知道问题出在哪了。。。
import os
#函数中的name是新建文件的名字,msg是写入的内容,类型为str类型,可任意传参
def txt_create(name,msg):
f_path = r’F:\python program\exercise\20190706’ # 新创建的txt文件的存放路径
full_path = f_path + name + ‘.txt’ #也可以创建一个.doc的word文档
file = open(full_path, ‘w’) # w 的含义为可进行读写
file.write(msg) # file.write()为写入指令
file.close()
def word_create(name,msg):
f_path = ‘F:\python program\exercise\20190706’ # 新创建的wrod文件的存放路径
full_path = f_path + name + ‘.docx’ #也可以创建一个.doc的word文档
file = open(full_path, ‘w’) # w 的含义为可进行读写
file.write(msg) # file.write()为写入指令
file.close()
if name==‘main’:
txt_create(‘hi’,‘my name is bill’)
word_create(‘hello’,‘what are doing?’)
运行结果如下: