python编写一程序,在c:\chfile目录下创建文件source1.txt,输入5行字符,文件内容任意(其中包含英文大、小写字符、数字字符 )
时间: 2024-03-08 11:47:25 浏览: 23
好的,以下是您所需的Python代码:
```python
import os
file_path = r'c:\chfile\source1.txt'
try:
with open(file_path, 'w') as f:
f.write('Hello World!\n')
f.write('This is a test file.\n')
f.write('12345\n')
f.write('AbCdEfG\n')
f.write('1a2b3c4d5e\n')
print('文件已成功创建并写入内容!')
except Exception as e:
print(f'创建文件失败:{e}')
```
请将以上代码保存至.py文件中,并运行程序即可在指定路径下创建文件source1.txt并输入5行字符。