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:\素材\03 python\导出属性字段.py", line 9, in <module>
    create_text('hello') # 调用函数
  File "E:\素材\03 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/'

 

相关文章:

  • 2021-12-10
  • 2021-06-08
  • 2021-11-12
  • 2021-11-09
  • 2021-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-18
  • 2021-08-22
  • 2021-12-08
  • 2021-12-24
  • 2021-08-06
  • 2021-12-28
  • 2021-08-24
相关资源
相似解决方案