a problem with shutil: the code won't work [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 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.

Related

How do I read the pictures in the file in order? [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 3 months ago.
Improve this question
I want to read the pictures in a file in the order they are in the file. But when I read it with python it reads mixed. I don't want it sorted. How can I fix this?
def read_img(path):
st = os.path.join(path, "*.JPG")
st_ = os.path.join(path, "*.jpg")
for filename in glob.glob(st):
print(st)
#print("filename-------",filename)
img_array_input.append(filename)
print("image array append : ", filename)
for filename in glob.glob(st_):
img_array_input.append(filename)
#print("filename-------",filename)
global size
size = len(img_array_input)
for i in img_array_input:
print("detection ")
detection(i)
print("detection out")
enter image description here
original file
enter image description here
the order of reading
I want it to read in the order in the original file.
If you want the list populated in the order that os.listdir() reveals files then:
from os import listdir
from os.path import join, splitext
BASE = '.' # directory to be parsed
EXTS = {'jpg', 'JPG', 'jpeg', 'JPEG'} # file extensions of interest
def ext(p):
_, ext = splitext(p)
if ext:
return ext[1:]
def getfiles(base, include_base=True):
for entry in listdir(base):
if ext(entry) in EXTS:
yield join(base, entry) if include_base else entry
detection = [file for file in getfiles(BASE, include_base=False)]
print(detection)
Note:
os.listdir() returns a list of files in arbitrary order

I couldn't make a list with these codes in 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 1 year ago.
Improve this question
I couldn't make a list with these codes in python
import os
path = r'C:\Users\fabri\Downloads\arquivos'
extension = '.csv'
for root, dirs_list, files_list in os.walk(path):
for file_name in files_list:
if os.path.splitext(file_name)[-1] == extension:
file_name_path = os.path.join(root, file_name)
print(file_name_path)
Use glob.glob() with the recursive=True option to get all the matching files in a folder and its subfolders.
from glob import glob
path = r'C:\Users\fabri\Downloads\arquivos'
file_list = glob.glob(os.path.join(path, '**/*.csv'), recursive=True)

Delete all files in a directory with a similar name? [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
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.

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)

file rename, python batch-processing [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'm new to coding, and only know VBA at best.
However I would like a python script which will rename all the excel files in a folder. It needs to include the date the file was last modified at the start of the string. The date format will be YYYYMMDD.
For example, if the original file name is "CWI001-CA-E1140", the ammended file name should be "20150422_CWI001-CA-E1140".
I'm using windows 8, thanks in advance.
Using os.listdir to get a list of file in a directory, and os.rename to rename files.
import datetime
import os
DIRECTORY = '.'
for filename in os.listdir(DIRECTORY):
# Skip non-xls file
if not filename.lower().endswith('.xls'):
continue
path = os.path.join(DIRECTORY, filename)
mtime = os.path.getmtime(path) # modification time
prefix = datetime.datetime.fromtimestamp(mtime).strftime('%Y%m%d')
newpath = os.path.join(DIRECTORY, prefix + '_' + filename)
# rename it
os.rename(path, newpath)

Categories

Resources