반응형 file, directory3 Python - with open 파일 쓰기 (file write) Code 1 2 3 4 file_name = 'write_file.txt' with open(file_name, 'w') as write_data: print(f"open file : {file_name}") write_data.write('WRITE TEST') cs Result #> python .\fie_open_write.py open file : write_file.txt #> cat .\write_file.txt WRITE TEST 2022. 12. 30. Python - folder 제거(remove) with os(import) Code 1 2 3 4 5 6 7 8 import os dir_path = 'D:\\new_folder' if os.path.exists(dir_path): os.rmdir(dir_path) print(f"removed folder: {dir_path}") else: print(f'There is no {dir_path}') cs Result #> python .\folder_remove.py removed folder: D:\new_folder 2022. 12. 29. Python - file 제거(remove) with os(import) Code 1 2 3 4 5 6 7 8 import os file = "remove_file.txt" if os.path.isfile(file): os.remove(file) print(f"removed file: {file}") else: print(f'There is no {file}') cs Result #> python .\file_remove.py There is no remove_file.txt #> echo 'creat' > remove_file.txt #> python .\file_remove.py removed file: remove_file.txt 2022. 12. 29. 이전 1 다음 반응형