python script for selecting and deleting files - python

I accidently made copies of a lot of files on my computer.
But one thing I noticed was that they all ended with the suffix ".copy", so in order to delete them I would like to write a python script to select these files and then deleting them.
How do i go about doing that?

import os
dir = 'C:\\Path\\To\\Directory' # if using Windows
#dir = '/path/to/directory' # if using Linux/OS X
files = [os.path.join(dir, f) for dir, subdir, files in os.walk(dir) for f in files if f.endswith('.copy')]
for f in files:
print f
# os.remove.path(f)
This will iterate through all files and folders starting at the root dir
Remove the hash tag # in front of os.remove.path after the first run once you've verified the correct files are being removed.

import os
for file_name in os.listdir("path/to/the/folder_with_files"):
if file_name.endswith('.copy'):
os. delete(file_name)
Hopes that work.

Related

Python loop through directories

I am trying to use python library os to loop through all my subdirectories in the root directory, and target specific file name and rename them.
Just to make it clear this is my tree structure
My python file is located at the root level.
What I am trying to do, is to target the directory 942ba loop through all the sub directories and locate the file 000000 and rename it to 000000.csv
the current code I have is as follow:
import os
root = '<path-to-dir>/942ba956-8967-4bec-9540-fbd97441d17f/'
for dirs, subdirs, files in os.walk(root):
for f in files:
print(dirs)
if f == '000000':
dirs = dirs.strip(root)
f_new = f + '.csv'
os.rename(os.path.join(r'{}'.format(dirs), f), os.path.join(r'{}'.format(dirs), f_new))
But this is not working, because when I run my code, for some reasons the code strips the date from the subduers
can anyone help me to understand how to solve this issue?
A more efficient way to iterate through the folders and only select the files you are looking for is below:
source_folder = '<path-to-dir>/942ba956-8967-4bec-9540-fbd97441d17f/'
files = [os.path.normpath(os.path.join(root,f)) for root,dirs,files in os.walk(source_folder) for f in files if '000000' in f and not f.endswith('.gz')]
for file in files:
os.rename(f, f"{f}.csv")
The list comprehension stores the full path to the files you are looking for. You can change the condition inside the comprehension to anything you need. I use this code snippet a lot to find just images of certain type, or remove unwanted files from the selected files.
In the for loop, files are renamed adding the .csv extension.
I would use glob to find the files.
import os, glob
zdir = '942ba956-8967-4bec-9540-fbd97441d17f'
files = glob.glob('*{}/000000'.format(zdir))
for fly in files:
os.rename(fly, '{}.csv'.format(fly))

Is there a way to change your cwd in Python using a file as an input?

I have a Python program where I am calculating the number of files within different directories, but I wanted to know if it was possible to use a text file containing a list of different directory locations to change the cwd within my program?
Input: Would be a text file that has different folder locations that contains various files.
I have my program set up to return the total amount of files in a given folder location and return the amount to a count text file that will be located in each folder the program is called on.
You can use os module in Python.
import os
# dirs will store the list of directories, can be populated from your text file
dirs = []
text_file = open(your_text_file, "r")
for dir in text_file.readlines():
dirs.append(dir)
#Now simply loop over dirs list
for directory in dirs:
# Change directory
os.chdir(directory)
# Print cwd
print(os.getcwd())
# Print number of files in cwd
print(len([name for name in os.listdir(directory)
if os.path.isfile(os.path.join(directory, name))]))
Yes.
start_dir = os.getcwd()
indexfile = open(dir_index_file, "r")
for targetdir in indexfile.readlines():
os.chdir(targetdir)
# Do your stuff here
os.chdir(start_dir)
Do bear in mind that if your program dies half way through it'll leave you in a different working directory to the one you started in, which is confusing for users and can occasionally be dangerous (especially if they don't notice it's happened and start trying to delete files that they expect to be there - they might get the wrong file). You might want to consider if there's a way to achieve what you want without changing the working directory.
EDIT:
And to suggest the latter, rather than changing directory use os.listdir() to get the files in the directory of interest:
import os
start_dir = os.getcwd()
indexfile = open(dir_index_file, "r")
for targetdir in indexfile.readlines():
contents = os.listdir(targetdir)
numfiles = len(contents)
countfile = open(os.path.join(targetdir, "count.txt"), "w")
countfile.write(str(numfiles))
countfile.close()
Note that this will count files and directories, not just files. If you only want files then you'll have to go through the list returned by os.listdir checking whether each item is a file using os.path.isfile()

Why does this Python script delete '.txt' files, as well as all the '.tmp' files?

I'm trying to write a script that will automatically delete all the temp files in a specific folder, and I noticed that this script also deletes all the text files in that folder as well. Can anyone explain why it does that?
import os
path = 'C:\scripts27'
for root, dirs, files in os.walk(path):
for currentFile in files:
print "processing file: " + currentFile
extensions=('.tmp')
if any(currentFile.lower().endswith(ext) for ext in extensions):
os.remove(os.path.join(root, currentFile))
I'm running this script using Python 2.7.10 on a Windows 8.1 PC 64-bit.
Thanks!
I'm assuming you meant providing a list of extensions. But in your case, extensions is defined as ('.tmp') which is not a tuple but a string. This causes your code to loop over all files and check for names ending with ., t, m and p thereby deleting your .txt files.
The fix here is to define extensions as ['.tmp'] or ('.tmp',) (notice the trailing ,).

Check a directory for all files of a certain filetype

I am having trouble checking a directory for all files of a certain filetype in python, .wav to be specific.
I have tried a few different methods to solve the problem but can't seem to tackle it. Is there any way in python to check a directory like this?
this will make a list of all the filenames and then check if they end with .wav
import os
listdirectory = os.listdir(".") # gets the name of all files in your dir
for filename in listdirectory:
if filename.endswith(".wav"): # check each of the files for whether or not they end in .wav
import os
files = os.listdir(path) #returns a list of files in the given directory
for filename in files:
if filename.endswith(".wav"):
doSomething
Use iglob:
import glob
search_for = '/foo/bar/*/*.wav'
for i in glob.iglob(search_for):
print(i)

Remove certain filetypes in Python

I am running a script that walks a directory structure and generates new files in each folder in the directory. I want to delete some of the files right after creation. This is my idea, but it is quite wrong I imagine:
directory = os.path.dirname(obj)
m = MeshExporterApplication(directory)
os.remove(os.path.join(directory,"*.mesh.xml"))
How to you put wildcards in a path? I guess not like /home/me/*.txt, but that is what I am trying.
Thanks,
Gareth
You can use the glob module:
import glob
glob.glob("*.mesh.xml")
to get a list of matching files. Then you delete them, one by one.
directory = os.path.dirname(obj)
m = MeshExporterApplication(directory)
# you can use absolute pathes in the glob
# to ensure, that you're purging the files in
# the right directory, e.g. "/tmp/*.mesh.xml"
for f in glob.glob("*.mesh.xml"):
os.remove(f)
do a for loop with the list of files as the thing you are looping over.
directory = os.path.dirname(obj)
m = MeshExporterApplication(directory)
for filename in os.listdir(dir):
if not(re.match(".*\.mesh\".xml ,filename) is None):
os.remove(directory + "/" + file)

Categories

Resources