1.Python 删除文件
2.思路:(1)确定文件路径
(2)os.listdir()获取当前文件下所有文件名称
(3)os.remove(file_data)调用删除文件
- import os
- import shutil
- #获取当前文件路径
- currenfil = os.getcwd()
- #指定文件路径
- path = currenfil + '/hm'
-
- #python删除文件的方法 os.remove(path)path指的是文件的绝对路径,如:
- def del_file(path_data):
- for x in os.listdir(path_data):
- file_data = path_data + '/' + x
- os.remove(file_data)
- del_file(path)
2.创建文件夹
- #获取当前文件路径
- currenfil = os.getcwd()
- #创建新文件
- path = currenfil + '/hm'
- if os.path.exists(path):
- print('文件已生成')
- else:
- os.mkdir(path)
3.创建文件,并写入数据
-
- currenfil = os.getcwd()
- #创建新文件
- path = currenfil + '/hm'
-
- full_path = path + '/my.text'
- file = open(full_path, 'w')
- file.write('输入文字\n')
- file.close()
4.读取指定text文件内容
- file = open(currenfil + '/test.text','r');
- array = file.readlines()