Advertisement
GamerBhai02

APP Exp 1

Mar 6th, 2025 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | Source Code | 0 0
  1.  
  2.     import os
  3.     file = open('demo.txt','w')
  4.     file.write("Hello World!")
  5.     file.close()
  6.     file = open('demo.txt','r')
  7.     print(file.read())
  8.     file.close()
  9.  
  10.     with open('demo.txt','a') as file:
  11.         file.write("\nHave a good day")
  12.     with open('demo.txt','r') as file:
  13.         print(file.read())
  14.  
  15.     with open('demo.txt','r') as file:
  16.         file.seek(6)
  17.         print(file.tell())
  18.         content = file.read()
  19.         print(content)
  20.         print(file.tell())
  21.  
  22.     import os
  23.     file = open('demo.bin','wb')
  24.     file.write(b"Hello World!")
  25.     file.close()
  26.     file = open('demo.bin','rb')
  27.     print(file.read())
  28.     file.close()
  29.  
  30.     with open('demo.bin','ab') as file:
  31.         file.write(b"\nHave a good day")
  32.     with open('demo.bin','rb') as file:
  33.         print(file.read())
  34.  
  35.     with open('demo.bin','rb') as file:
  36.         file.seek(6)
  37.         print(file.tell())
  38.         content = file.read()
  39.         print(content)
  40.         print(file.tell())
  41.  
  42.     import zipfile
  43.     with zipfile.ZipFile("zipped_files.zip",'w') as zipf:
  44.         zipf.write("demo.txt")
  45.         zipf.write("demo.bin")
  46.         print("Files Zipped Successfully")
  47.  
  48.     with zipfile.ZipFile("zipped_files.zip",'r') as zipf:
  49.         zipf.extractall("unzipped_files")
  50.         print("Files Unzipped Successfully")
  51.  
  52.     if os.path.exists('demo.txt'):
  53.         os.remove('demo.txt')
  54.         print("File Deleted")
  55.     else:
  56.         print("File doesn't exist")
  57.  
  58.     if os.path.exists('demo.bin'):
  59.         os.remove('demo.bin')
  60.         print("File Deleted")
  61.     else:
  62.         print("File doesn't exist")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement