How to delete files and directories in python

This article describes how to delete files and directories in python. Share with everyone for your reference. The specific implementation method is as follows:

os.remove(path)

Delete file path. If path is a directory, OSError will be thrown. If you want to delete a directory, use rmdir().

remove() has the same function as unlink()

In Windows, deleting a file in use will throw an exception. In Unix, the records in the directory table are deleted, but the file storage is still there.

# Use os.unlink()And os.remove()To delete files
#! /user/local/bin/python2.7
# - *- coding:utf-8-*-import os
my_file ='D:/text.txt'if os.path.exists(my_file):
 # To delete files, you can use the following two methods.
 os.remove(my_file)
 # os.unlink(my_file)else:
 print 'no such file:%s'%my_file

**os.removedirs(path) **

Delete directories recursively. Similar to rmdir(), if the subdirectory is successfully deleted, removedirs() will delete the parent directory; but the subdirectory is not successfully deleted, an error will be thrown.

For example, os.removedirs("foo/bar/baz") will delete the "foo/bar/ba" directory first, then delete foo/bar and foo, if they are empty

If the subdirectory cannot be deleted successfully, an OSError exception will be thrown

os.rmdir(path)

To delete the directory path, the path must be an empty directory, otherwise OSError will be thrown

Recursively delete directories and files (similar to the DOS command DeleteTree):

Copy the code code as follows:

import os
for root, dirs, files in os.walk(top, topdown=False):for name in files:
 os.remove(os.path.join(root, name))for name in dirs:
 os.rmdir(os.path.join(root, name))

Method 2:

code show as below

import shutil
shutil.rmtree()

Example extension:

Python os.unlink() method

The os.unlink() method is used to delete a file, and an error is returned if the file is a directory.

The following example demonstrates the use of the unlink() method:

#! /usr/bin/python
# - *- coding: UTF-8-*-import os, sys
# List directories
print "The directory is: %s"%os.listdir(os.getcwd())
os.unlink("aa.txt")
# Deleted directory
print "The deleted directory is: %s"%os.listdir(os.getcwd())

The directory is:
[ ‘a1.txt’,’aa.txt’,’resume.doc’]
The deleted directory is:
[ ‘a1.txt’,’resume.doc’ ]

So far, this article on how to delete files and directories in python is introduced. For more related methods of deleting files and directories in python, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support more in the future ZaLou.Cn!

Recommended Posts

How to delete files and directories in python
How to delete cache files in python
How to use and and or in Python
How to save text files in python
How to read and write files with Python
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to write return in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to represent null values in python
How to write win programs in python
How to run id function in python
How to custom catch errors in python
How to write try statement in python
How to delete redundant kernels in Ubuntu
How to delete redundant kernels in Ubuntu
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
How to use the round function in python
How to modify time zone and time in ubuntu
How to use the zip function in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
How to use the format function in python
How to set code auto prompt in python
How to install the downloaded module in python
Python how to delete rows with empty columns
How to write a confession program in python
How does Python store data to json files
How to add and delete users on CentOS 8
How to perform continuous multiplication calculation in python
How to repackage Deb files under Debian and Ubuntu
How to understand the introduction of packages in Python
How to understand a list of numbers in python
How to modify time zone and time in ubuntu system
Example of how to automatically download pictures in python
How to save IE as an attachment in python
How to create a Python virtual environment in Ubuntu 14.04
Python implements FTP to upload files in a loop
What are the ways to open files in python
How to comment python code
How to learn python quickly
How to uninstall python plugin
3 ways to encrypt Python files
How to understand python objects
How to use python tuples
Generators and iterators in Python
How to find the area of a circle in python
How to install memcache and start it in ubuntu environment
How to compile and install PHP and Nginx in Ubuntu environment