Working with Files#

Syntax#

Attribuut

Mode

Description

“r”

Read

Default value. Opens a file for reading, error if the file does not exist

“a”

Append

Opens a file for appending, creates the file if it does not exist

“w”

Write

Opens a file for writing, creates the file if it does not exist

“x”

Create

Creates the specified file, returns an error if the file exists

“t”

Text

Default value. Text mode

“b”

Binary

Binary mode (e.g. images)

Example:

1#!/usr/bin/env python3
2
3def main():
4    f = open("demofile.txt")
5
6
7if __name__ == "__main__":
8    main()

Example:

1#!/usr/bin/env python3
2
3def main():
4    f = open("demofile.txt", "rt")
5
6
7if __name__ == "__main__":
8    main()

Reading files#

Example:

1#!/usr/bin/env python3
2
3def main():
4    f = open("demofile.txt", "r")
5    print(f.read())
6
7
8if __name__ == "__main__":
9    main()

Example:

1#!/usr/bin/env python3
2
3def main():
4    f = open("../demofile.txt", "r")
5    print(f.read())
6
7
8if __name__ == "__main__":
9    main()

Read only part of the file

Example:

1#!/usr/bin/env python3
2
3def main():
4    f = open("demofile.txt", "r")
5    print(f.read(5))
6
7
8if __name__ == "__main__":
9    main()

Read lines

Example:

1#!/usr/bin/env python3
2
3def main():
4    f = open("demofile.txt", "r")
5    print(f.readline())
6
7
8if __name__ == "__main__":
9    main()

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    f = open("demofile.txt", "r")
 5    print(f.readline())
 6    print(f.readline())
 7
 8
 9if __name__ == "__main__":
10    main()

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    f = open("demofile.txt", "r")
 5    for x in f:
 6        print(x)
 7
 8
 9if __name__ == "__main__":
10    main()

Closing#

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    f = open("demofile.txt", "r")
 5    print(f.readline())
 6    f.close()
 7
 8
 9if __name__ == "__main__":
10    main()

Writing files#

open and read the file after the appending:

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    f = open("demofile2.txt", "a")
 5    f.write("Now the file has more content!")
 6    f.close()
 7
 8    #open and read the file after the appending:
 9    f = open("demofile2.txt", "r")
10    print(f.read())
11
12
13if __name__ == "__main__":
14    main()

open and read the file after the appending

Example:

 1#!/usr/bin/env python3
 2
 3def main():
 4    f = open("demofile3.txt", "w")
 5    f.write("Woops! I have deleted the content!")
 6    f.close()
 7
 8    f = open("demofile3.txt", "r")
 9    print(f.read())
10
11
12if __name__ == "__main__":
13    main()

Create a new file#

“x”, “a”, “w”

Example:

1#!/usr/bin/env python3
2
3def main():
4    f = open("myfile.txt", "x")
5
6
7if __name__ == "__main__":
8    main()

Example:

1#!/usr/bin/env python3
2
3def main():
4    f = open("myfile.txt", "w")
5
6
7if __name__ == "__main__":
8    main()

Delete a file or folder#

Example:

 1#!/usr/bin/env python3
 2
 3import os
 4
 5def main():
 6    os.remove("demofile.txt")
 7
 8
 9if __name__ == "__main__":
10    main()

Example:

 1#!/usr/bin/env python3
 2
 3import os
 4
 5def main():
 6    if os.path.exists("demofile.txt"):
 7        os.remove("demofile.txt")
 8    else:
 9        print("The file does not exist")
10
11
12if __name__ == "__main__":
13    main()

Example:

 1#!/usr/bin/env python3
 2
 3import os
 4
 5def main():
 6    os.rmdir("demodir")
 7
 8
 9if __name__ == "__main__":
10    main()