Delete all files in a directory with a similar name? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
so I am currently somewhat new in python, usually, I do programming in Node.js. I wanted to know if there is a way to delete all files that has a specific name in them?
For example, let's say I have the following files in a directory:
PDforeg.txt
PDahvn.txt
AHgme.txt
Ronra.txt
I want to be able to delete all files that includes the word "PD" in them. How do I do this?

import glob
import os
files = glob.glob("PATH_to_directory/*.txt")
for file in files:
if "PD" in file:
os.remove(file)

import subprocess
subprocess.run('rm *PD*.txt', shell=True)
Or you could import os and do os.remove() stuff.

For multi platform you can use the following script:
import glob
import os
folder = '/home/user/Documents/'
text_to_look_for = 'PD'
for f in glob.glob(folder + '*' + text_to_look_for +'*.txt'):
os.remove(f)

import pathlib
path = pathlib.Path('D:/dev/stackoverflow') # This is your folder
[p.unlink() for p in path.glob('*PD*')]
# Another way of writing the above line
for p in path.glob('*PD*'):
p.unlink()
*PD* will be all files that have PD in them somewhere, not just at the start. If you want to remove files with PD only at the start, use PD*.

In bash, just,
[user ~]$rm *.txt
This command will delete all the files with the same .txt extension in the current directory.

Related

a problem with shutil: the code won't work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 hours ago.
Improve this question
import os
import shutil
source = r'/Users/***/Downloads/test1'
destination = r'/Users/***/Downloads/test2'
files = os.listdir(source)
files2 = os.listdir(destination)
for file in files:
if os.path.isfile(file):
shutil.copy(file, destination)
It just doesn't copy anything. It worked but then just crashed or smth. Now it doesn't say that there's any mistake in the code, but it doesn't do anything
Try this to debug your code
import os
import shutil
source = r'/Users/***/Downloads/test1'
destination = r'/Users/***/Downloads/test2'
files = os.listdir(source)
files2 = os.listdir(destination)
print("files:",files)
for file in files:
print("Testing whether ", file, " is a file")
if os.path.isfile(file):
print("Yes, so attempting to copy file", file)
shutil.copy(file, destination)
It will help you see a clearer picture of what is going wrong. If you can reply with the information printed when you run it, that will help people find the problem.

how to copy many files inside of a directory to many other directories using shell or python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i have many files inside a directory1(libX11.so.6, tm.txt, ff.txt..and so on), i need to copy these files of directory1 to many other directories named as directory2,3.......100000. I tried find command but its not working.My sample online code is given below.can anybody suggest some better solutions using shell or python...Thanks in advance.
find . -type f -name "libX11.so.6, tm.txt, ff.txt" -exec cp -rv {} /home/geo/data/directory{2...100000} \;
while doing this it shows errors
The following code will copy all your original_dir files to the destination_dir.
I believe you can do some customized changes to get what you want.
import os
from shutil import copyfile
original_dir = "/path/to/the/original/dir"
original_files = os.listdir(original_dir)
destination_dir = "/path/to/your/destination/dir"
for each in original_files:
current_file = os.path.join(original_dir, each)
# check if it is a file
if not os.path.isdir(current_file):
# print(each + " is being processing")
copyfile(current_file, destination_folder + each)
You could use the python library shutil to achieve this, code to make this work is below:
import shutil
shutil.copy(src, dst)

How do I get the path of a specific file in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I need to get the path of a file. How do I do it? I tried this but it didn't work:
import subprocess
import os
file_name = "something.exe"
path = os.path.dirname(file_name)
print path
subprocess.call(path)
ok so what you want to do is to first locate the file with a os.walk() function:
import os
from os.path import join
lookfor = "file_name.exe"
for root, dirs, files in os.walk('C:\\'):
if lookfor in files:
# found one!
path = os.path.join(root,lookfor)
print(path) #this is the path you required
subprocess.call(path)
break
but this will take a while and run the first file it found. also I assumed you have a windows, otherwise you should replace 'C:\' with '/'.
This should work as far as I understand your question:
path = os.path.abspath(file_name)
print path
It takes a relative path and returns the absolute system path.
For me it just makes no sense. If the filename is all you have, it won't find the file anyways. If file_name is your relative path to the file, the abspath() function should work.
Please use the os module :https://docs.python.org/2/library/os.html
import os
file = "/Users/Test.py"
print(os.path.abspath(file))

Python/Powershell/Batch script to move file from one location to another via Parameter for file name? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I would like to implement within my python script, a process to move files from one location to another, based off of parameter that will be inserted to get the file name (ideally a wildcard) .. I am going to implement this into a loop and it file names will change as it loops through, so i will want to be able to pick these files up based off their names.
Any help is appreciated.
Thanks.
Edit:
Now I have taken the suggestion from Trotta (thanks) and have a code sample which I have tried to use. I have made it into a python function with a parameter. The parameter will be constantly changing within the loop, also I do not need to rename the file, just simply move it from one directory to another. However it does not appear to be picking it up. I have created source directory 'release_deployment_scripts' and the target directory 'test_delete' within the root directory of my python project/environment but it's not picking any up. FYI the 2 files I have in the source folder are both 'PostDeploy_BI-Test.sql' and 'PreDeploy_BI_Test.sql' ... do you know what i'm doing wrong? Code below:
import glob
import shutil
def add_deployment_files(Jira):
list_of_files = glob.glob('/release_deployment_scripts/*' + Jira + '*') #source file(s)
for file in list_of_files:
print(file)
new_path = '/test_delete/' # target destination
shutil.move(file, new_path)
add_deployment_files('BI-Test')
One approach to do that in python would be to work with glob and shutil. You could fetch specific files with glob (that allows the use of wildcards) and move them to another path that you define beforehand:
import glob
import shutil
list_of_files = glob.glob('/path/to/directory/B*') # you can work with wildcards here; in this example you would fetch all files in this directory starting with a 'B'
for file in list_of_files:
new_name = '/new/path/filename' # create new name for the file
shutil.move(file, new_name)
As it is not apparent from your question how you would want to implement this into a loop, I assume you'll be able to adapt this snippet to your needs.

How to convert IDLE python files into a ZIP folder? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an assignment which needs me to convert 4 separate py files into a zip folder. Do you guys know how to do this?
This is the part of the instruction which tells me to compress it
Programs: Name your programs, q1.py, q2.py, q3.py, q4.py and submit all as a zip file(named A1_my_upi.zip) to the assignment drop box before the deadline (No late submission
accepted).
I have read on the internet that I have to import zipfile. Can some one please clairfy?
Let's say that your files are in /users/matthew/documents/school/assignments/a1. Here are a couple of ways in which you can do this:
Python:
import os
from zipfile import Zipfile
dirpath = '/users/matthew/documents/school/assignments/a1'
with open("A1_my_upi.zip", 'w') as outfile:
for fname in os.listdir(dirpath):
if not fname.endswith('.py'):
continue
outfile.write(os.path.join(dirpath, fname))
Now, you have a A1_my_upi.zip that you can submit
If you're on a linux/mac computer, you can do this:
$ cd /users/matthew/documents/school/assignments/a1
$ cd ..
$ zip A1_my_upi.zip a1/*.py
Now, you have a A1_my_upi.zip that you can submit. It exists at /users/matthew/documents/school/assignments/A1_my_upi.zip

Categories

Resources