It is currently Thu Jun 27, 2024 10:04 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: reading a folder of text files (problem solved)
PostPosted: Sat Mar 28, 2020 10:51 am  (#1) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
GIMP Version: 2.10.18 samj
Operating System: Windows
GIMP Experience: Moderate



I have a bit of experience in writing plug-ins in python for image manipulation, but none in browsing text files from the plug-in.
I prepared a folder on my desktop called MyPresets.
I wrote a test filter with the following instructions (copied somewhere on the net)
def LoadPresets (inFolder) :
   
    home = expanduser("~")
    i_dir = home+inFolder
    gimp.message (str(i_dir))
   
    # Read every file in directory
    for filename in os.listdir(i_dir):
        gimp.message (str(filename))

        #with open(filename, "r") as f:
        with open(filename,"r") as f:    ### DOES NOT FIND IT ?????
            # Read each line of the file
            for line in f.readlines():
                gimp.message (line.split())   
   
    return

The inFolder field contains "\Desktop\MyPresets"
I got the messages I wrote for tracing but then I got the error as shown:
Attachment:
Cattura.PNG
Cattura.PNG [ 36.43 KiB | Viewed 2230 times ]


Why the file is not found?

Sorry for my ignorance in that type of instructions.

Any help?

_________________
"Where am I ?"


Last edited by dinasset on Sat Mar 28, 2020 4:17 pm, edited 1 time in total.

Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: reading a folder of text files (solved)
PostPosted: Sat Mar 28, 2020 3:15 pm  (#2) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
self solution?

it seems that I have to enter
with open(i_dir+filename,"r") as f:
i.e. repeating the full path

quite surprising for an ignorant like me, but if this is the solution, OK...

_________________
"Where am I ?"


Top
 Post subject: Re: reading a folder of text files (solved)
PostPosted: Sat Mar 28, 2020 5:45 pm  (#3) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4750
dinasset wrote:
self solution?

it seems that I have to enter
with open(i_dir+filename,"r") as f:
i.e. repeating the full path

quite surprising for an ignorant like me, but if this is the solution, OK...


os.listdir() gives you the names of the files in the listed directory, so yes, you have to add the directory to that to access the file. This is best done using os.path.join(dir,file) (because the right separator for the OS will be added automatically).

Alternatively you can use glob.glob() that allows you to specify a file pattern:
import glob
files=glob.glob(os.path.join(dir ,'*.txt'))

_________________
Image


Top
 Post subject: Re: reading a folder of text files (problem solved)
PostPosted: Sat Mar 28, 2020 11:47 pm  (#4) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Thanks!
In my ignorance on the subject I thought that -once accessed thru the listdir- the "path" was "remembered" by the python filter.
Ok, I can proceed now.

_________________
"Where am I ?"


Top
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) Reading XCF

3

No new posts Attachment(s) Reading multiple paged PDF - extracting images from

24

No new posts Attachment(s) Iterate through a folder of images

7

No new posts Folder for Exposure plugin

8

No new posts Attachment(s) missing plug in folder

4



* Login  



Powered by phpBB3 © phpBB Group