“Plik usuwa Python” Kod odpowiedzi

Jeśli plik istnieje, usuń Python

import os
filePath = '/home/somedir/Documents/python/logs'

if os.path.exists(filePath):
    os.remove(filePath)
else:
    print("Can not delete the file as it doesn't exists")
Perro Fiel

Plik usuwa Python

import os
import shutil

if os.path.exists("demofile.txt"):
  os.remove("demofile.txt") # one file at a time

os.rmdir("test_directory") # removes empty directory
shutil.rmtree("test_directory") # removes not empty directory and its content
Charming Crab

Python OS Usuń plik

import os
os.remove("filename.txt")
Duco Defiant Dogfish

Python Usuń zapisany obraz

import os
os.remove(file) for file in os.listdir('path/to/directory') if file.endswith('.png')
Robin R

Python usuń zawartość pliku

open('file.txt', 'w').close()
Horrible Hornet

Plik usuwa Python

import os
os.remove("/tmp/<file_name>.txt")
Intempestive Al Dente

Odpowiedzi podobne do “Plik usuwa Python”

Pytania podobne do “Plik usuwa Python”

Więcej pokrewnych odpowiedzi na “Plik usuwa Python” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu