avatarBetter Everything

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3125

Abstract

the same and only change the filename.</p><div id="a861"><pre><span class="hljs-keyword">import</span> shutil

path = <span class="hljs-string">'C:/Users/BE/Documents'</span> current_name = <span class="hljs-string">'bunny.jpg'</span> new_name = <span class="hljs-string">'rabbit.jpg'</span>

current_file = path+<span class="hljs-string">'/'</span>+current_name new_file = path+<span class="hljs-string">'/'</span>+new_name

shutil.move(current_file,new_file)</pre></div><p id="f9d6">After running the code, there is no <code>bunny.jpg</code> file anymore, but there is now a <code>rabbit.jpg</code> file containing the same image.</p><p id="b3ef">Since we worked with an absolute filepath the location of the Python script does not matter.</p><p id="4ec3"><b>Warning: </b>If there already is a file with the new filename, the existing file will be overwritten.</p><p id="4b3a">I believe you can also use this method to change the filetype from txt to csv for example. But changing the filetype of an image requires another method. I wrote a short article about it:</p><div id="535c" class="link-block"> <a href="https://readmedium.com/converting-images-to-other-file-types-small-python-projects-3-c0aa6911ef79"> <div> <div> <h2>Converting Images to other File Types — Small Python projects #3</h2> <div><h3>Different scenarios require different image types. Learn how to convert images to other file types like: PNG, JPG or…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*699T6hNgFt3SEgdaN-HsTA.jpeg)"></div> </div> </div> </a> </div><h2 id="8591">Deleting a file</h2><p id="d09f">Now we will take a look at how we can delete a file. Our test subject will be <code>python.jpg</code> located at <code>'C:/Users/BE/Documents'</code>.</p><p id="af20">To delete a file you can use the <code>remove</code> function from the <code>os</code> package. All you have to do is specify the filepath.</p><div id="5191"><pre><span class="hljs-keyword">import</span> os

os.remove(<span class="hljs-string">'C:/Users/BE/Documents/python.jpg'</span>)</pre></div><p id="45bf">After running the code, the file can no longer be found in the directory.</p><p id="7ecd">And again, since we worked with an absolute filepath the location of the Python script does not matter.</p><h1 id="b7a5">Thank you for reading!</h1><p id="d614">You can <b>get full access to all my posts by joining Medium</b>. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:</p><div id="80fc" class="link-block"> <a href="https://medium.com/@BetterEverything/membership"> <div> <div> <h2>Join Medium with my referral link — Better Everything</h2> <div><h3>Read every story from Better Everything (and thousands of other writers on Medium). Your membership fee directly…</

Options

h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*aa4Y_6MHVoY6Wl-9)"></div> </div> </div> </a> </div><p id="d7ef">You might also like:</p><div id="bdf4" class="link-block"> <a href="https://readmedium.com/automate-incoming-file-processing-with-python-df7daca0e7ff"> <div> <div> <h2>Automate Incoming File Processing with Python</h2> <div><h3>I will show you how you can automatically process files after they enter a certain file folder with a Python directory…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*qUpNn-d9WLM69Wkz8KIugA.jpeg)"></div> </div> </div> </a> </div><div id="f978" class="link-block"> <a href="https://readmedium.com/automate-removal-of-old-files-in-python-2085381fdf51"> <div> <div> <h2>Automate Removal of Old Files in Python</h2> <div><h3>I explain how to build a Python program that checks if the files in a directory are older than a threshold and if so…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*fwDsvAbuFKi3f1zVnLRt9g.png)"></div> </div> </div> </a> </div><div id="bb9e" class="link-block"> <a href="https://readmedium.com/passing-data-to-a-python-file-when-running-it-with-commands-3cc437667b2b"> <div> <div> <h2>Passing Data to a Python File when Running It with Commands</h2> <div><h3>Feed input data into your Python script by using command line arguments and collecting them in your Python program.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*lhn4RrOGYuv3mSc0D4f_zg.jpeg)"></div> </div> </div> </a> </div><div id="e194" class="link-block"> <a href="https://readmedium.com/get-name-size-type-date-of-files-in-python-d8b4f972a348"> <div> <div> <h2>Get Name, Size, Type & Date of Files in Python</h2> <div><h3>This article explains how to get an overview of a directory’s filenames and how to get the size, type and date of files…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*gOsOJAUVO1RtPyZBrv9UzA.png)"></div> </div> </div> </a> </div></article></body>

Move, Rename & Delete Files in Python

In this article I will show you 3 useful operations for managing files in Python:

  1. Moving a file to another directory
  2. Renaming a file
  3. Deleting a file
Moving, renaming and deleting files are useful operations for managing files in Python. Image by catalyststuff on Freepik

Moving a file to another directory

To move a file from one directory to another we can use the move function from the shutil package.

It takes 2 arguments:

  1. The filepath including filename of the file that has to be moved
  2. The filepath that specifies the destination including the filename

As an example we will move the file Order0001.csv from the directory New to the directory Processed.

import shutil

current_path = 'New/Order0001.csv'
destination_path = 'Processed/Order0001.csv'

shutil.move(current_path,destination_path)

After running the code, the Order0001.csv file is not in the New directory anymore, but it is in the Processed directory.

The above code was written in a script in a directory that also contains the New and Processed directories, that is why our relative filepaths worked. To learn more about filepaths, you can read this article:

Warning: If there already is a file with the destination filepath, the existing file will be overwritten.

Renaming a file

Suppose we have a file called bunny.jpg located at: 'C:/Users/BE/Documents'. Now we just want to change the name to rabbit.jpg.

To rename a file we can also use the move function from the shutil package. This time however, we keep the path the same and only change the filename.

import shutil

path = 'C:/Users/BE/Documents'
current_name = 'bunny.jpg'
new_name = 'rabbit.jpg'

current_file = path+'/'+current_name
new_file = path+'/'+new_name

shutil.move(current_file,new_file)

After running the code, there is no bunny.jpg file anymore, but there is now a rabbit.jpg file containing the same image.

Since we worked with an absolute filepath the location of the Python script does not matter.

Warning: If there already is a file with the new filename, the existing file will be overwritten.

I believe you can also use this method to change the filetype from txt to csv for example. But changing the filetype of an image requires another method. I wrote a short article about it:

Deleting a file

Now we will take a look at how we can delete a file. Our test subject will be python.jpg located at 'C:/Users/BE/Documents'.

To delete a file you can use the remove function from the os package. All you have to do is specify the filepath.

import os

os.remove('C:/Users/BE/Documents/python.jpg')

After running the code, the file can no longer be found in the directory.

And again, since we worked with an absolute filepath the location of the Python script does not matter.

Thank you for reading!

You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:

You might also like:

Software Development
Automation
Data Engineering
Python
Programming
Recommended from ReadMedium