在 Julia 中打开和读取文件
Julia 中的文件处理是使用 open()、read()、close() 等函数实现的。有很多方法可以读取文件的内容,例如 readline()、readlines() 和 read()。
- open():打开一个存在于绝对路径中的文件,作为参数提供。
- read():将文件内容读入单个字符串。
- close():关闭文件对象或保存已打开文件实例的变量。
打开文件
考虑与 Julia 的实现文件一起放置在同一工作目录中的文件“text.txt”。
测试.txt
one
two
three
four
five
six
现在我们将使用open()函数以可读模式打开文件
句法 :
f = open("absolute path of the file", "r")
# few file operations
close(f)
使用open()函数打开文件有两种方法:
方法一
使用 open() 打开文件并将其分配给作为打开文件实例的变量,然后使用该变量对打开的文件执行进一步的操作。然后使用 close() 关闭文件。
Python3
# Opening a file in read_mode, method 1
# r is the default mode
f = open("test.txt", "r")
# do some file operations
# close the file instance
close(f)
Python3
# opening a file in read_mode, method 2
open("test.txt") do f
# do stuff with the open file instance 'f'
end
# opened file is automatically closed after the do control ends.
Python3
# read file contents, one line at a time
open("test.txt") do f
# line_number
line = 0
# read till end of file
while ! eof(f)
# read a new / next line for every iteration
s = readline(f)
line += 1
println("$line . $s")
end
end
Python3
# opening a file in read_mode
# r is the default mode
f = open("test.txt", "r")
# to count total lines in the file
line_count = 0
for lines in readlines(f)
# increment line_count
global line_count += 1
# print the line
println(lines)
end
# total lines in file
println("line count is $line_count")
close(f)
Python
# opening a file in read_mode
f = open("test.txt", "r")
# read entire file into a string
s = read(f, String)
print(s)
close(f)
Python3
# opening a file in read_mode
# dummy.txt doesn't exist
# in the working directory
f = open("dummy.txt", "r")
close(f)
Python3
# since the presence of file is uncertain we'll use try catch
try
open("dummy.txt", "r") do s
# perform desired operations if file exists
end
catch
# either warn or print that the file doesn't exist
println("file doesn't exist")
end
方法二
使用“执行”控制流打开一个文件。一旦执行循环结束,打开的文件将自动关闭。在 do 控制流中执行所需的操作。这是打开和访问文件的最常见和最方便的方式。
Python3
# opening a file in read_mode, method 2
open("test.txt") do f
# do stuff with the open file instance 'f'
end
# opened file is automatically closed after the do control ends.
读取文件的内容
使用 Julia 中的预定义函数可以通过多种方式读取文件。文件可以逐行读取,以字符串的形式读取,也可以一次读取整个文件。
使用 readline()函数逐行读取文件内容
假设一个文件中有 n 行。我们可以使用 readline()函数逐行(一次一行)读取文件的内容,直到达到 EOF(文件结束)。
Python3
# read file contents, one line at a time
open("test.txt") do f
# line_number
line = 0
# read till end of file
while ! eof(f)
# read a new / next line for every iteration
s = readline(f)
line += 1
println("$line . $s")
end
end
输出:
使用 readlines() 将文件的行读入字符串数组
readlines() 方法用于将文件的所有行读入一个字符串数组。每个元素都是文件的一行。使用它,我们可以一次读取多行。
Python3
# opening a file in read_mode
# r is the default mode
f = open("test.txt", "r")
# to count total lines in the file
line_count = 0
for lines in readlines(f)
# increment line_count
global line_count += 1
# print the line
println(lines)
end
# total lines in file
println("line count is $line_count")
close(f)
输出:
使用 read() 一次将文件的所有内容读入字符串
使用 read() 方法将文件内容完全读入单个字符串。
句法:
s = read(f, String)
# f can be a file object or an absolute path
Python
# opening a file in read_mode
f = open("test.txt", "r")
# read entire file into a string
s = read(f, String)
print(s)
close(f)
输出:
文件异常处理
到目前为止,我们已经假设了我们的文件存在于指定路径中的情况。如果指定路径中不存在文件怎么办?
Python3
# opening a file in read_mode
# dummy.txt doesn't exist
# in the working directory
f = open("dummy.txt", "r")
close(f)
输出:
解决方案:
我们将使用 Julia 中的 Try-Catch 块来处理这个问题。使用 open() 在 try 块中打开文件,如果文件存在,则执行所需的操作。否则 try 块将抛出异常并且控制到达 catch 块。可以使用 println() 消息或@warn 在 try 块中警告用户。
Python3
# since the presence of file is uncertain we'll use try catch
try
open("dummy.txt", "r") do s
# perform desired operations if file exists
end
catch
# either warn or print that the file doesn't exist
println("file doesn't exist")
end
输出: