OpenFile
Opens text file using the specified mode, returns file ID.
Syntax
OpenFile(filePath, mode)
Parameters
Parameter |
Type |
Description |
---|---|---|
filePath |
string |
Full path of the file. |
mode |
string |
File open mode. Can be either ‘r’, ‘rt’ (read), ‘w’ or ‘wt’ (write) or ‘a’ or ‘at’ (append) |
Return
Returns file ID.
Note
See Specifying Windows file paths in Python for details on specifying file paths with backslashes in Python.
Examples
Python
# open a file in read mode
f = open("C:\\Data\\parameters.txt", "r")
# read all the lines in the file and print them
if f:
lines = f.readlines()
f.close()
for line in lines:
print(line.rstrip()) # rstrip() removes end-of-line character from line
NexScript
% open a file in read mode
file = OpenFile("C:\Data\parameters.txt", "r")
% read all the lines in the file and print them
if file > 0
line = "" % make line a string variable
while ReadLine(file, line) > 0
Trace(line)
end
CloseFile(file)
end