Lua loadfile not finding a file - file

I had some lua code with the following line:
JSON = loadfile("JSON.lua")()
The file JSON.lua is in the same directory as the lua code that line came from. This code worked for me for a while, and then, without my changing either the lua source, or the JSON.lua, or permission of any of the files, or the directory from where I was running the lua code, I started getting a nil error on that line. (I simply recall NO relevant changes that could have any impact on the lua code.)
Adding an assert revealed that the error was caused by the file not being found. Playing with file permissions, restarting my machine didn't resolve the issue, and pulling back code that I had checked in and was working perfectly did not resolve the error.
I resolved the error by changing the line above to provide the absolute path to that JSON.lua file.
Is there anything explaining why the code without the absolute path could have worked for a while and then stopped working?
Note: This behavior of working and then not working happened to me twice over a week. I am puzzled and though I have now found a fix, I am really curious as to the explanation for that intermittent behavior.

Lua uses package.path, whose default value comes from the environment variable LUA_PATH if it is set, as the list of directories to search. You can put . of the front of this list to load files from the current directory, or you can put your files in a path on the list.

A late answer on this, as I found exactly the same problem.
First, contrary to the previous answer, loadfile doesn't use the package.path search path. It only looks in the specified directory. And if you don't specify a directory, it only look in the 'current directory'. I can't explain exactly why it stopped working for you, but probably your Lua code is somehow being run with a different 'current directory' than previous.
There are two possible fixes: One is to specify an absolute path to loadfile.
JSON = loadfile("c:\\my_folder\\JSON.lua")()
The alternative fix depends on the particular library you're using, which I suspect is Jeffrey Friedl's Lua JSON lilbrary. Because this supports the newer Lua module mechanism, you can just load the module with require, which does support the package.path search path.
JSON = require("JSON")

Related

Could not find file 'C:\Users\crims\AppData\Local\Microsoft\VisualStudio\10.0\ProjectAssemblies\...\MonitoringSystemDatabase.mdb'

I have a Menu Form and it has 7 UserControls every time I execute my program I keep seeing this error.
I have no idea what error is this. I tried searching but there are no error close related to mine. Then I compare my error with this Project Assemblies error
Can anyone help me with this? I have no idea what causes this error.
The error is caused by your code looking for the MS Access database file called:
MonitoringSystemDatabase.mdb
...is located at path:
C:\Users\crims\AppData\Local\Microsoft\VisualStudio\10.0\ProjectAssemblies\cjz5kapb01\
...but it is not.
Locate file MonitoringSystemDatabase.mdb (perhaps with +E to Search), note the actual path of the file, and correct the path in MainMenu.vb.
The error message is very clear, so why all the searching?
Either the file isn't there, or the application can't see it where it expects to find it.
Try moving the file to:
C:\Users\Public\Documents\MonitoringSystemDatabase.mdb
and adjust you project accordingly.

Not able to find external files

I'm a rookie programmer and am having trouble directing my compiler to find certain image files using const char.
I'm reading through a book, "Programming 2D Games" by C. Kelly, and when I look through his
code, he uses this line to find his image file.
const char NEBULA_IMAGE[] = "pictures\NasaNebula.jpg"; // photo source nasaimages.org
When I do this I get an error that I cannot find the file. However if I use this line:
const char NEBULA_IMAGE[] = "E:/TestProject/pictures/NasaNebula.jpg"; //Photo source nasa
It works. Could someone let me know how I can configure my project so that it can find these files without defining their exact path? I've looked around for a while but can't find exactly what I need.
Thank you
Since that path is used at runtime (not at compile time) to locate and load that file you have to make sure that such a relative path makes sense from the current situation. You have to make sure that the current working directory of your application is such, that from that directory on the relative path leads to the file.
In your specific example the applications working directiory must be in E:/TestProject, so that the relative path pictures/NasaNebula.jpg leads to the full (and correct) path E:/TestProject/pictures/NasaNebula.jpg.
In general relative path offer a lot of flexibility. For example by using diffferent resource folders at runtime, thus using different files without having to change the source code of the application. But relative path also demand that the current situation of the app allows to resolve such paths.

I have a .c.save file. What is it? What does it do?

I am programming with C using Code::Blocks. My project is divided in 3, header, implementation and main.
Whenever I used a project, apart from the source files and the bin and obj folders I had a .depend and a .layout file. All good.
Now I created a new project, and just copied -> pasted everything new in source files. I did this twice.
For each case, I have a .c.save file, which has the same name of the implementation file (ie. the implementation file is called imp, then the file is called imp.c.save). I asked a friend of mine what it might be, and he said I need to beware as he had two random files created, which prevented him from building correctly (he got a stupid error). When the files were deleted everything went back to normal.
I did a short test of the program and I can find nothing different. I am hesitant to delete it since this cropped up twice in two cases, but I don't want to compromise my coding.
Tried to google and I didn't find much. Any help?
Well, it didn't cause any problems so I assuming it is an autosave file.

ndk The filename or extension is too long

I am trying to build a shared object for Android using ndk-build command, the source compiles fine but then I get this error:
make (e=206): The filename or extension is too long.
If any body can tell me what causes this problem and how to fix it.
Thanks.
You hit the Windows command length limit. You should use some static libraries as a workaround. Typically, people compile branches of their source tree with separate Android.mk files that end with include $(BUILD_STATIC_LIBRARY), and then list these as $(LOCAL_STATIC_LIBRARIES) in the "main" jni/Android.mk that ends with include $(BUILD_SHARED_LIBRARY). Your ndk-build will load this "main" makefile, so it should include (explicitly or using some nesting approach) all the static library makefiles.
But this is only a convenience. You can achieve the same result if you use single jni/Android.mk file as you have now.
You may also find it easier to list the static libraries as $(LOCAL_WHOLE_STATIC_LIBRARIES) - this way you guarantee that the order of listing these libraries will not cause linking problems.
You can add this to Application.mk
APP_SHORT_COMMANDs :=true
This worked for me.
Maybe as a workaround, you can try to subst the directory "D:\MyFiles\Android\Datte\obj\local\armeabi\objs\ngspice\spicelib" for a drive letter, using:
subst X: "D:\MyFiles\Android\Datte\obj\local\armeabi\objs\ngspice\spicelib"
This could save some space and generate a smaller command line. However, it might not solve your problem, depending on the Windows command length limit, as Alex Cohn answered. Besides, you'll have to change your makefile and change, for example,
D:/MyFiles/Android/Datte//obj/local/armeabi/objs/ngspice/spicelib\parser\inp2y.o
for
X:\parser\inp2y.o

How to find the "current" source file in Python 3?

What's the simplest way to find the path to the file in which I am "executing" some code? By this, I mean that if I have a file foo.py that contains:
print(here())
I would like to see /some/path/foo.py (I realise that in practice what file is "being executed" is complicated, but I think the above is well defined - a source file that contains some function that, when executed, gives the path to said file).
I have needed this in the past to make tests (that require some external file) self-contained, and I am currently wondering if it would be a useful way to locate some support files needed by a program. But I have never found a good way of doing this. The inspect module sounds like it should work, but you seem to need a class or function that is defined in that module.
In particular, the module instances contain __file__ attributes, but I can't see how to get the "current" module. Objects have a __module__ attribute, but that's the module name, not a module instance.
I guess one way is to throw and catch an exception and inspect the contents, but that seems like hard work. Surely there is a simple, easy way that I have missed?
To get the absolute path of the current file:
import os
os.path.abspath(__file__)
To get content of external file distributed with your package you could use pkg_util.get_data()(stdlib) or pkg_resources.resouce_string() (setuptools) to support execution from zip-archives or standalone executables created by py2exe, PyInstaller or similar, example.

Resources