Nasal library/os.path: Difference between revisions
Red Leader (talk | contribs) (Doc rename()) |
Red Leader (talk | contribs) (Start realpath doc) |
||
Line 167: | Line 167: | ||
}} | }} | ||
==== realpath ==== | ==== realpath ==== | ||
{{Nasal doc | |||
|syntax = os.path.realpath; | |||
|text = A variable that is a string and gives the full file path. This should only be used when the path is an absolute path. | |||
|example1 = var path = os.path.new(getprop("/sim/fg-home")); | |||
print(path.realpath); # prints the absolute path of $FG_HOME | |||
}} | |||
==== file ==== | ==== file ==== | ||
==== dir ==== | ==== dir ==== |
Revision as of 19:05, 15 April 2018
The FlightGear forum has a subforum related to: Nasal Scripting |
Nasal scripting |
---|
Nasal internals |
---|
Memory Management (GC) |
This page contains documentation for the os.path
namespace in Nasal. This namespace implements tools (from SGPath) for manipulating file paths. Everything in the os.path
namespace is sourced from flightgear/src/Scripting/NasalSGPath.cxx
Tip Copy & paste the examples into your Nasal Console and execute them to see what they do. |
Note Everything here was added to FlightGear 3.0. (released February 2014) |
Class
os.path
A pseudo-class (actually a Nasal ghost). This is the main class that stores and allows manipulation of file paths. This is also integrated into the os.path
.
new()
os.path.new([path]);
Constructor function. Returns a new os.path
instance. Note that the path must not violate the rules for allowed paths.
- path
- Optional string specifying the filepath. If not given, the path will default to $FG_ROOT
Example
var path = os.path.new("C:/Windows"); # Windows filepath
# var path = os.path.new("/usr/bin/"); # Unix filepath
print(path.realpath);
set()
os.path.set([path]);
Sets the filepath. Note that the path must not violate the rules for allowed paths.
- path
- Optional string specifying the filepath. If not given, the path will be set to $FG_ROOT
Example
var path = os.path.new("C:/Windows");
# var path = os.path.new("/usr/bin/"); # alternative Unix filepath
print(path.realpath);
path.set("C:/Windows/Program Files");
# path.set("/usr/"); # alternative Unix filepath
print(path.realpath); # prints new path
append()
os.path.append([path]);
Appends a new part to the filepath.
- path
- Optional string specifying the new part to add to the filepath. If not given, the resultant path will not change.
Example
var path = os.path.new("C:/Windows");
# var path = os.path.new("/usr/"); # alternative Unix filepath
print(path.realpath);
path.append("Program Files");
# path.append("bin"); # alternative Unix filepath
print(path.realpath); # prints "C:/Windows/Program Files" or "/usr/bin"
concat()
os.path.concat([path]);
Appends a new part to the filepath.
- path
- Optional string specifying part to concat to the existing filepath. Note that this will simply add the string to the end of the path without adding a separator. If not given, the resultant path will not change.
Example
var path = os.path.new("C:/Windows");
# var path = os.path.new("/usr"); # alternative Unix filepath
print(path.realpath);
path.concat("/Program Files");
# path.concat("/bin"); # alternative Unix filepath
print(path.realpath); # prints "C:/Windows/Program Files" or "/usr/bin"
exists()
os.path.exists();
Returns 1 (true) if the path exists and 0 (false) if it does not.
Example
var path = os.path.new("C:/Windows");
# var path = os.path.new("/usr"); # alternative Unix filepath
print(path.exists()); # prints 1 (true)
path.set("C:/Window");
# path.set("/r"); # alternative Unix filepath
print(path.exists()); # prints 0 (false)
canRead()
os.path.canRead();
Returns 1 (true) if the path can be read (from Nasal) and 0 (false) if it cannot. This follows the rules for allowed paths.
Example
var path = os.path.new("C:/Windows");
# var path = os.path.new("/usr"); # alternative Unix filepath
print(path.canRead()); # prints 0 (false)
path.set(getprop("sim/fg-home"));
print(path.canRead()); # prints 1 (true)
canWrite()
os.path.canWrite();
Returns 1 (true) if the path can be written to (from Nasal) and 0 (false) if it cannot. This follows the rules for allowed paths.
Example
var path = os.path.new("C:/Windows");
# var path = os.path.new("/usr"); # alternative Unix filepath
print(path.canWrite()); # prints 0 (false)
path.set(getprop("sim/fg-home") ~ "/txt.log");
print(path.canWrite()); # prints 1 (true)
isFile()
os.path.isFile();
Returns 1 (true) if the path points to a file and 0 (false) if it points to a directory. Note that the path must also exist.
Example
var path = os.path.new(getprop("sim/fg-root"));
print(path.isFile()); # prints 0 (false)
path.append("Nasal/io.nas");
print(path.isFile()); # prints 1 (true)
isDir()
os.path.isDir();
Returns 1 (true) if the path points to a directory and 0 (false) if it points to a file. Note that the path must also exist.
Example
var path = os.path.new(getprop("sim/fg-root"));
print(path.isDir()); # prints 1 (true)
path.append("Nasal/io.nas");
print(path.isDir()); # prints 0 (false)
isRelative()
os.path.isRelative();
Returns 1 (true) if the path is relative and 0 (false) if it is not.
Example
var path = os.path.new(getprop("sim/fg-root"));
path.set("./Nasal");
print(path.isRelative()); # prints 1 (true)
isAbsolute()
os.path.isAbsolute();
Returns 1 (true) if the path is an absolute path and 0 (false) if it is not.
Example
var path = os.path.new(getprop("sim/fg-root"));
print(path.isAbsolute()); # prints 1 (true)
isNull()
os.path.isNull();
Returns 1 (true) if the path is null and 0 (false) if it is not.
Example
var path = os.path.new();
print(path.isNull());
create_dir()
os.path.create_dir();
Creates the parent of the path as a directory. This will only work if the path is absolute and considered as writable by fgValidatePath()
(typically okay under $FG_HOME/Export).
Example
var path = os.path.new(getprop("/sim/fg-home") ~ "/Export/test/demo.txt");
path.create_dir(); # Creates $FG_HOME/Export/test/
remove()
os.path.remove();
Removes the last element of the path from the file directory. This will only work if the path is absolute and deletion of the file is permitted by the file permissions (typically okay under $FG_HOME/Export). Note that on Windows, deletion of read-only files are never permitted.
Examples
var path = os.path.new(getprop("/sim/fg-home") ~ "/Export/demo.txt");
var file = io.open(path.realpath, "w");
io.write(file, 'Hello, World' ~ "\n");
io.close(file);
print(path.exists()); # prints "1"
path.remove();
print(path.exists()); # prints "0"
var path = os.path.new(getprop("/sim/fg-home") ~ "/Export/test");
path.create_dir();
path.remove(); # folder "/test" is removed
rename()
os.path.rename(new_name);
Renames the last element of the path from the file directory. This will only work if the path is absolute, the old path is both readable and writable, the new path is writable.
Example
var path = os.path.new(getprop("/sim/fg-home") ~ "/Export/demo.txt");
var file = io.open(path.realpath, "w");
io.write(file, 'Hello, World' ~ "\n");
io.close(file);
path.rename(getprop("/sim/fg-home") ~ "/Export/demo1.txt");
print(path.realpath); # prints $FG_HOME/Export/demo1.txt
realpath
os.path.realpath;
A variable that is a string and gives the full file path. This should only be used when the path is an absolute path.
Example
var path = os.path.new(getprop("/sim/fg-home"));
print(path.realpath); # prints the absolute path of $FG_HOME