Assign input string to directory

Hello,

I would like to assign string to a directory and then save images in it. I did:


name2 = input("Enter path for thresholded images: ")

results = name2 #path for saving thresholded binary images
os.mkdir(results) #create directory

plt.imshow(binary, cmap = 'gray')
plt.savefig(name2 + "binary%s.jpg" % frame, dpi = 300)

Unfortunately, the file are not saved in the directory but on the desktop. Any idea about how to solve this problem?

Thanks in advance,
Antonio

There are two solutions i can think of.
After creating the directory you could os.chdir so it becomes the default.
The other way is to add the directory in the path so save the file into.


plt.savefig(results + “/“ + name2 + "binary%s.jpg" % frame, dpi = 300)

Thanks. I found another way:

plt.savefig(f"{name2}/binary%s.jpg" % frame, dpi = 300)

I do not know what the ‘f’ does(means), but it works.

Using an “f-string” allows you to insert data from a variable or expression into a string, by enclosing it in curly braces, the same way the “%s” does. You could rewrite that statement as:

plt.savefig(f"{name2}/binary{frame}.jpg", dpi = 300)

This is just another way to create the path string.

The problem with your initial attempt is that you just create a file name without indicating a path properly. Say for example that you input folder, so that your program will create a folder named folder on the desktop. First off, the reason it’s going on the Desktop (and that your images are) is because that happens to be the current working directory. You need to understand how this works - and it’s a general “how computers work” thing rather than a Python thing.

Anyway, the code name2 + "binary%s.jpg" % frame will just create filenames like folderbinary1.jpg. There’s no way that the operating system can know that you meant to put a file named binary1.jpg into the folder folder, because folderbinary1.jpg is a perfectly valid file name. Instead, you need a string that contains a path separator. / will work on all systems (it may not be what your operating system uses “natively”, but it will get translated somewhere in between automatically). So you should save the file with the “name” (really a path) folder/binary1.jpg instead.


Now that we understand how path strings work, we should study the techniques used for building a string.

When you use f"{name2}/binary%s.jpg" % frame, the / is the main reason why it worked: it made sure the string we’re building is correct. But the special f syntax is an important technique for the actual work of building the string. It’s what puts the name2 value into the result in this code, instead of joining pieces together with the + operator. Or you can say, it’s another way to do the thing that % frame is doing to add the frame ID to the name.

The f syntax is overall easier to use than %, and avoids some nasty gotchas. It’s recommended for modern code. To simplify the code, we can use it for putting all the pieces together. That looks like: f"{name2}/binary{frame}.jpg".

I have a detailed write-up of these techniques here:

Thanks for your help.