[RESOLVED] file io.open from folder

Hi,

I created a lot of files I want to be able to read from a folder, using io.open. Something like :

local path = system.pathForFile(“folder/myFile.txt”)

seems not to work.

Is this possible?

[import]uid: 148841 topic_id: 34832 reply_id: 334832[/import]

I’m pretty sure it does work but you have to say “folder.myFile.txt” instead of using /
I haven’t actually tried using folders with system.pathForFile but when using the Require function it uses “folder.file” so I assume its the same.
Sorry if I’m wrong :confused: [import]uid: 14461 topic_id: 34832 reply_id: 138497[/import]

Hi @Jack01,
Can you please post a bit more of your code surrounding this? Also, show me approximately how you have your files (and directory to read from) set up? And finally, if you’re receiving any errors in the Terminal?

Thanks,
Brent [import]uid: 200026 topic_id: 34832 reply_id: 138500[/import]

And are you building for iOS or Android? [import]uid: 199310 topic_id: 34832 reply_id: 138536[/import]

Hi!

Thx for the replies. I build for iOS.

It concerns multilanguage word files, which I would like to put in a folder “luaLocales”.

_G.wordlist is determined by the language preference of the user.

So if I set

 \_G.wordlist = "luaLocales.wordlist\_NL.txt"   

I get an error “attempt to index global ‘luaLocales’ (a nil value)”
Same with “luaLocales/wordlist_NL.txt”.

 function buildDictionary()  
 local path = system.pathForFile(\_G.wordList)  
 local file = io.open( path, "r")  
 if file then  
 for line in file:lines() do  
 dictTable[line] = true  
 end  
 io.close(file)  
 end  
 end  

It works fine when not putting the txt files in a folder. [import]uid: 148841 topic_id: 34832 reply_id: 138781[/import]

Hi @Jack01,
So this a text file that you create in advance and want to place inside the “luaLocales” folder within your project directory? In this case, your first method using the “/” is proper.

local path = system.pathForFile( "luaLocales/wordlist\_NL.txt" )

Because it’s reportedly not, there might be something else going on here. As a test, try specifying that file path directly, not referencing from the global variable. Then print out the value of “path” and see if it comes back non-nil.

Brent [import]uid: 200026 topic_id: 34832 reply_id: 138818[/import]

Hi Brent,

Thanks for your reply. Sry, I must have made a typo, cuz now it works fine.

local path = system.pathForFile( "luaLocales/wordlist\_NL.txt" )

[import]uid: 148841 topic_id: 34832 reply_id: 138866[/import]

Great to hear! Let me know if you encounter any other issues with this.

Brent [import]uid: 200026 topic_id: 34832 reply_id: 138896[/import]

I’m pretty sure it does work but you have to say “folder.myFile.txt” instead of using /
I haven’t actually tried using folders with system.pathForFile but when using the Require function it uses “folder.file” so I assume its the same.
Sorry if I’m wrong :confused: [import]uid: 14461 topic_id: 34832 reply_id: 138497[/import]

Hi @Jack01,
Can you please post a bit more of your code surrounding this? Also, show me approximately how you have your files (and directory to read from) set up? And finally, if you’re receiving any errors in the Terminal?

Thanks,
Brent [import]uid: 200026 topic_id: 34832 reply_id: 138500[/import]

And are you building for iOS or Android? [import]uid: 199310 topic_id: 34832 reply_id: 138536[/import]

Hi!

Thx for the replies. I build for iOS.

It concerns multilanguage word files, which I would like to put in a folder “luaLocales”.

_G.wordlist is determined by the language preference of the user.

So if I set

 \_G.wordlist = "luaLocales.wordlist\_NL.txt"   

I get an error “attempt to index global ‘luaLocales’ (a nil value)”
Same with “luaLocales/wordlist_NL.txt”.

 function buildDictionary()  
 local path = system.pathForFile(\_G.wordList)  
 local file = io.open( path, "r")  
 if file then  
 for line in file:lines() do  
 dictTable[line] = true  
 end  
 io.close(file)  
 end  
 end  

It works fine when not putting the txt files in a folder. [import]uid: 148841 topic_id: 34832 reply_id: 138781[/import]

Hi @Jack01,
So this a text file that you create in advance and want to place inside the “luaLocales” folder within your project directory? In this case, your first method using the “/” is proper.

local path = system.pathForFile( "luaLocales/wordlist\_NL.txt" )

Because it’s reportedly not, there might be something else going on here. As a test, try specifying that file path directly, not referencing from the global variable. Then print out the value of “path” and see if it comes back non-nil.

Brent [import]uid: 200026 topic_id: 34832 reply_id: 138818[/import]

Hi Brent,

Thanks for your reply. Sry, I must have made a typo, cuz now it works fine.

local path = system.pathForFile( "luaLocales/wordlist\_NL.txt" )

[import]uid: 148841 topic_id: 34832 reply_id: 138866[/import]

Great to hear! Let me know if you encounter any other issues with this.

Brent [import]uid: 200026 topic_id: 34832 reply_id: 138896[/import]