This question seems really easy, but I could not figure that out. I want to save a jpg file. I used plt.savefig(fileName). I also create a new folder using import os and then os.mkdir('D:\Users\data'). Now, I want to put this figure into this created folder. Thanks in advance ...
Just pass the full path to savefig.
folderName = 'D:/Users/data'
os.makedirs(folderName)
plt.savefig(os.path.join(folderName, fileName))
Related
I'am really new to this python scripting thing, but pretty sure, that there is the way to copy files from one folder (via given path in .txt file) to another.
there would be directly path to the folder, which contains photo files
I'am working with huge amounts of photos, which contains gps metadata (so i need not to lose it).
Really apreciate any help, thanks.
Here is a short and simple solution:
import shutil
import os
# replace file_list.txt with your files
file_list = open("file_list.txt", "r")
# replace my_dir with your copy dir
copy_dir = "my_dir"
for f in file_list.read().splitlines():
print(f"copying file: {f}")
shutil.copyfile(f, f"{copy_dir}/{os.path.split(f)[1]}")
file_list.close()
print("done")
It loops over all the files in the file list, and copies them. It should be fast enough.
I am trying to write image in my opencv code, which is fine if i write without directory. But when I am trying to write in directory, it run but does not write in directory.
for i in xrange(3):
path = 'resultImages/result'
print os.path.join(path,str(i),'.png')
cv.imwrite(os.path.join(path,str(i),'.png'),images[i*3+2])
Anything wrong here?
I reffered OpenCV - Saving images to a particular folder of choice but no help.
The problem is due to the fact you are using ".png" as a sub directory inside the os.path.join() function
Try changing it to this:
for i in xrange(3):
path = 'resultImages/result'
print os.path.join(path,str(i) + '.png')
cv.imwrite(os.path.join(path,str(i) +'.png'),images[i*3+2])
I hope it helped
I want to use Python to move .xlsx files if they are in a certain folder.
Is there a generic rule for this?
What I am looking for is basically code for this:
if there is a file that is an excel document in /user/documents/folder:
move it to trash.
I'm happy with the shutil.move bit but what can I use to classify .xlsx files as a group?
Thank you for your help everyone. This turned out to be the easiest way of doing this
import os
folder = os.listdir(r'C:\Users\name\Desktop\somefolder')
for item in folder:
if item.endswith('.xlsx') == True:
print ('excel folder is in this folder')
I can then add a shutil.move function with ""if item.endswith('xlsx') == True""
to move the file elsewhere.
Thanks for the support I.Renk and others :)
I have two directories:
dir = path/to/annotations
and
dir_img = path/to/images
The format of image names in dir_img is image_name.jpg.
I need to create empty text files in dir as: image_name.txt, wherein I can later store annotations corresponding to the images. I am using Python.
I don't know how to proceed. Any help is highly appreciated. Thanks.
[Edit]: I tried the answer given here. It ran without any error but didn't create any files either.
This should create empty files for you and then you can proceed further.
import os
for f in os.listdir(source_dir):
if f.endswith('.jpg'):
file_path = os.path.join(target_dir, f.replace('.jpg', '.txt'))
with open(file_path, "w+"):
pass
You can use the module os to list the existing files, and then just open the file in mode w+ which will create the file even if you're not writing anything into it. Don't forget to close your file!
import os
for f in os.listdir(source_dir):
if f.endswith('.jpg'):
open(os.path.join(target_dir, f.replace('.jpg', '.txt')), 'w+').close()
In my code, I create a textfile for the stdout and also save several .png images and .mat matrices - when the code finishes running there are a lot of files inside the directory
I want the code to be able to create a new directory inside the folder where my code is running, and save the .txt file as well as the output .png and .mat to this newly created folder.
I have figured out that to create the new directory I should do:
import os
os.mkdir('folder')
And to create the output file and set the stdout there it is
import sys
filename = open('filename.txt','w')
sys.stdout = filename
I tried using open('folder/filename.txt','w') but i get the error: IOError: [Errno 2] No such file or directory
Thank you!
If I understand you right, you want to create the file 'filename.txt' inside the folder you just made ('folder')?
Given that's the case, use os.path.join()
import sys
filename = open(os.path.join('folder','filename.txt'),'w')
sys.stdout = filename
Now sys.stdout points to the file which is inside the new folder