def savefile(path, content): if os.path.exists(path): try: print("...writig content into the file") temp_file = open(path, mode="w") temp_file.write(content) temp_file.close() print("...succesfully written content into the file: {0}".format(path)) except: print("...ERROR: failed to write content into the file: {0}".format(path)) else: print("...creating file") jtwin.createfile(path) if os.path.exists(path): print("...created file succesfully") print("...writing content into the file") try: temp_file = open(path, mode="w") temp_file.write(content) temp_file.close() print("...succesfully written content into file: {0}".format(path)) except: print("...ERROR: failed to write content into file: {0}".format(path)) else: print("...ERROR: failed to create file: {0}".format(path)) return 0
def ask_savefile(content): result = ask_for("save the result in a seperate file?", "> ", ["yes", "y", "no", "n"]) if result in ["y", "yes"]: temp_path = input("path: ") if not (os.path.exists(temp_path)): print("...creating file") jtwin.createfile(temp_path) if os.path.exists(temp_path): print("...created file succesfully") print("...writing content into the file") try: temp_file = open(temp_path, mode="w") temp_file.write(content) temp_file.close() print("...succesfully written content into file: {0}".format(temp_path)) except: print("...ERROR: failed to write content into file: {0}".format(temp_path)) else: print("...ERROR: failed to create file: {0}".format(temp_path)) return 0 else: if os.path.getsize() > 50: result = ask_for( "the file already inherits content, override or append to it?", "> ", ["override", "append"] ) if result == "append": try: print("...appending content to the file") temp_file = open(temp_path, mode="r") temp_cache = temp_file.read() temp_file.close() temp_file = open(temp_path, mode="w") temp_file.write(temp_cache + "\n" + content) temp_file.close() print("..succesfully appended content to the file: {0}".format(temp_path)) except: print("...ERROR: couldnt append content to the file: {0}".format(temp_path)) elif result == "override": try: print("...overriding the file with the content") temp_file = open(temp_path, mode="w") temp_file.write(temp_cache + "\n" + content) temp_file.close() print("...succesfully overridden the file: {0}".format(temp_path)) except: print("...ERROR: couldnt override the content of the file: {0}".format(temp_path)) else: try: print("...writing content to the file") temp_file = open(temp_path, mode="w") temp_file.write(temp_cache + "\n" + content) temp_file.close() print("...succesfully written conten into the file: {0}".format(temp_path)) except: print("...ERROR: couldnt write content to the file: {0}".format(temp_path))
def createfile(filepath): """ creates a file independant from the used operating system ### filepath - (string) the string of the file to create ### RETURNS (void) """ operating_system = platform.system() if operating_system == "Linux": jtlinux.createfile(filepath) elif operating_system == "Windows": jtwindows.createfile(filepath)