File Operations

General file operations including listing files, opening or closing a file, and moving cursor within a file.

List Files

To list files and sub-directories within a directory, use the files function. It returns next information of files.

  • File names.

  • A directory or not. (0:file , 1:directory)

  • Size of files.

  • Last accessed time.

  • Last modified time.

files("C:/DolphinDB");
filename isDir fileSize lastAccessed lastModified
LICENSE_AND_AGREEMENT.txt 0 22558 2017.05.23 03:04:35.000 2017.05.23 03:04:35.000
README_WIN.txt 0 5104 2017.05.23 03:04:35.000 2017.01.08 09:03:52.680
server 1 0 2017.06.05 01:08:52.437 2017.06.05 01:08:52.437
THIRD_PARTY_SOFTWARE_LICENS... 0 8435 2017.05.23 03:04:35.000

2017.01.05 15:00:26.506

files("C:/DolphinDB", "readme%");
filename isDir fileSize lastAccessed lastModified
README_WIN.txt 0 5104 2017.05.23 03:04:35.000 2017.01.08 09:03:52.680

Alternatively, we can use SQL expressions to manipulate the returned table.

select * from files("C:/DolphinDB") where filename like "DolphinDB%";
filename isDir fileSize lastAccessed lastModified
DolphinDB 1.lnk 0 824 2017.07.13 08:13:22.000 2017.07.18 02:27:06.000
DolphinDB 2.lnk 0 790 2017.07.13 08:14:45.000 2017.08.04 07:36:45.000
DolphinDB 3.lnk 0 1006 2017.08.04 06:54:26.000 2017.08.04 07:36:53.000
DolphinDB 4(acl).lnk 0 872 2017.08.04 06:50:02.000 2017.08.13 12:09:50.412

Open and Close Files

The file function can open a file with a given mode. The opening mode could be one of the 6 modes: "r", "r+", "w", "w+", "a", and "a+". (For details please see file )The close function closes an opened file handle.

fout=file("C:/DolphinDB/test.txt","w");
fout.writeLine("hello world!");
// output: 1

fout.close();
fin = file("C:/DolphinDB/test.txt");
print fin.readLine();
// output: hello world!

fin.close();

In the example below, a file is opened within a function and it will be automatically closed when the function call completes. We don't have to explicitly close the file in many cases unless it is necessary to close the file immediately. When the system shuts down, all open files will be closed.

def myread(f): file(f).readLine()
myread("C:/DolphinDB/test.txt");
// output: Hello World!

Move Cursor within a File

When the system reads data from a file or writes data to a file, the internal cursor moves forward. Users can manipulate the cursor manually via the seek function. In addition to the file handle, the seek function accepts another 2 arguments: offset and the starting position. The offset could be positive or negative and the starting position must be of one the 3 positions: HEAD, CURRENT, and TAIL. The seek function returns the final position of the internal cursor if no exception is raised.

// write a function to show the length of a file
def fileLength(f): file(f).seek(0, TAIL)
fileLength("C:/DolphinDB/test.txt");
// output: 14

// move the internal cursor to the beginning of the file
fin=file("C:/DolphinDB/test.txt")
fin.readLine();
// output: Hello World!
fin.seek(0, HEAD);
// output: 0
fin.readLine();
// output: Hello World!