Reputation: 831
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.
Upvotes: 1
Views: 2998
Reputation: 142
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
Upvotes: 4
Reputation: 711
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()
Upvotes: 1