Ch 14 - Files Flashcards
Think Julia
How do you write to a file?
To write a file, you have to open it with mode “w” as a second parameter:
julia > fout = open(“ output.txt”, “w”)
IOStream( < file output.txt >)
If the file already exists, opening it in write mode clears out the old data and starts fresh, so be careful! If the file doesn’t exist, a new one is created. open returns a file object and the write function puts data into the file:
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5381-5390). Kindle Edition.
Think Julia
What happens if you use open() to open a file in write mode, but the file already exists?
To write a file, you have to open it with mode “w” as a second parameter:
julia > fout = open(“ output.txt”, “w”)
IOStream( < file output.txt >)
If the file already exists, opening it in write mode clears out the old data and starts fresh, so be careful! If the file doesn’t exist, a new one is created. open returns a file object and the write function puts data into the file:
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5381-5390). Kindle Edition.
Think Julia
How can you write to a file?
fout = open(“output123.txt”, “w”)
line1 = “This here’s the wattle,\n”
write(fout, line1)
line2 = “the emblem of our land.\n”
write(fout, line2)
close(fout)
Think Julia
What should you do after you have finished working with a file?
When you are done writing, you should close the file:
julia > close( fout)
If you don’t close the file, it gets closed for you when the program ends.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5401-5406). Kindle Edition.
Think Julia
What type of data will the write() function accept?
The argument of write has to be a string, so if we want to put other values in a file, we have to convert them to strings. The easiest way to do that is with string or string interpolation:
julia > fout = open(“ output.txt”, “w”)
IOStream( < file output.txt >)
julia > write( fout, string( 150))
3
An alternative is to use the print or print( ln) functions:
julia > camels = 42
42
julia > println( fout, “I have spotted $ camels camels.”)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5407-5422). Kindle Edition.
Think Julia
What is an alternative to the write() function for writing to a file?
The argument of write has to be a string, so if we want to put other values in a file, we have to convert them to strings. The easiest way to do that is with string or string interpolation:
julia > fout = open(“ output.txt”, “w”)
IOStream( < file output.txt >)
julia > write( fout, string( 150))
3
**An alternative is to use the print or print( ln) functions: **
julia > camels = 42
42
julia > println( fout, “I have spotted $camels camels.”)
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5407-5422). Kindle Edition.
Think Julia
How can you find the current directory?
Files are organized into directories (also called “folders”). Every running program has a “current directory,” which is the default directory for most operations. For example, when you open a file for reading, Julia looks for it in the current directory.
The function pwd returns the name of the current directory:
julia > cwd = pwd()
“/ home/ ben”
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5427-5434). Kindle Edition.
Think Julia
How can you find the absolute path?
A path that begins with / does not depend on the current directory; it is called an absolute path. To find the absolute path to a file, you can use abspath:
julia > abspath(“ memo.txt”)
“/ home/ ben/ memo.txt”
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5441-5446). Kindle Edition.
Think Julia
What is a: ispath()
Julia provides other functions for working with filenames and paths. For example, ispath checks whether a file or directory exists:
julia > ispath(“ memo.txt”)
true
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5447-5450). Kindle Edition.
Think Julia
What is a: isdir()
If it exists, isdir checks whether it’s a directory:
julia > isdir(“ memo.txt”)
false
julia > isdir(“/ home/ ben”)
true Similarly,
isfile checks whether it’s a file.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5451-5457). Kindle Edition.
Think Julia
What is a: isfile()
If it exists, isdir checks whether it’s a directory:
julia > isdir(“ memo.txt”)
false
julia > isdir(“/ home/ ben”)
true Similarly,
isfile checks whether it’s a file.
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5451-5457). Kindle Edition.
Think Julia
What is a: readdir()
readdir returns an array of the files (and other directories) in the given directory:
julia > readdir( cwd)
3-element Array{ String, 1}:
“memo.txt”
“music”
“photos”
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5461-5462). Kindle Edition.
Think Julia
What happens if you try to open a file that does not exist (not in write mode)?
A lot of things can go wrong when you try to read and write files. If you try to open a file that doesn’t exist, you get a SystemError:
julia > fin = open(“ bad_file”)
ERROR: SystemError: opening file “bad_file”: No such file or directory
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5485-5487). Kindle Edition.
Think Julia
How can you deal with all the possible errors that can occure opening a file?
To avoid these errors you could use functions like ispath and isfile, but it would take a lot of time and code to check all the possibilities.
It’s easier to go ahead and try, and deal with problems if they happen— which is exactly what the try statement does. The syntax is similar to an if statement:
try fin = open(“ bad_file.txt”)
catch exc
println(“ Something went wrong: $ exc”)
end
Ben Lauwens and Allen B. Downey. thinkjulia (Kindle Locations 5492-5500). Kindle Edition.