1 def create_text(filename): 2 path = '/Users/admin/Desktop/' #需自定义路径 3 file_path = path + filename + '.txt' 4 file = open(file_path,'w') 5 file.close() 6 7 create_text('hello') # 调用函数
注意第2行代码,表示的是在桌面新建hello.txt文件。建议写完整路径
>>> Traceback (most recent call last): File "E:素材 3 python导出属性字段.py", line 9, in <module> create_text('hello') # 调用函数 File "E:素材 3 python导出属性字段.py", line 7, in create_text file = open(file_path,'w') IOError: [Errno 2] No such file or directory: '/Users/JN-PC/Desktop/hello.txt' >>>
path = 'C:/Users/admin/Desktop/'