def print_file(dir, file): path = Path(dir + '/' + file) if path.exists(): try: with path.open('r') as fd: print(fd.read()) except IOError as err: print(err)
def create_file(dirname): file_name = "mafile.txt" file_path = Path(dirname + '/' + file_name) try: fd = file_path.open('w+') fd.write("This is the most useless file you have ever seen...") fd.close() except IOError as err: print(err) return file_name
def create_dir(): dirname = "useless_dir" path = Path(dirname) try: if path.exists() is False: path.mkdir() except OSError as err: print("Creation of the directory {dir} failed".format(dir=dirname)) print(err) return dirname
def ft_make_dir_and_file(): p = Path(l) if not p.dirs(d): new_d = p / d new_d.mkdir() else: new_d = p / d + '/' new_f = new_d / f new_f.touch() new_f.write_text("Hello!!! World!!!\n") print(new_f.text())
def run(): path = Path("local_lib/") if (path.dirs("folder")) == []: newdir = path + "folder" + "/" newdir.mkdir() else: newdir = path + "folder" + "/" newfile = newdir + "my_file" newfile.touch() newfile.write_text("Hello world! Text writed!") print (newfile.text())
def func(): dossier_a_creer = "nouveau_dossier" fichier_a_creer = "nouveau_fichier.txt" my_dir = Path(dossier_a_creer) #my_dir = my_dir / 'tmp' #my_dir = my_dir / 'tmp/tmp' NE MARCHE: je passe a autre chose !!! """ est ce dir qu il existe """ if not my_dir.isdir(): my_dir.mkdir() my_new_file = my_dir / fichier_a_creer """ est que le fichier existe """ if not my_new_file.isfile(): my_new_file.touch() my_new_file.write_text( "\n1ere piscine python: QuelLe galere ce jour!!!\n2eme piscine python: Cela va un peu mieux...\n\n" ) print(my_new_file.text())
#!/usr/local/bin/python3 from local_lib.path import Path if __name__ == '__main__': d = Path('./tmp') d.mkdir_p() f = Path('./tmp/test') f.touch() f.open() f.write_text("some texts and more texts") print(f.text())
from local_lib.path import Path if __name__ == '__main__': folder = Path('./forex01') folder.mkdir_p() file = Path('./forex01/forex01.txt') file.touch() file.open() file.write_text("For ex01 text") print(file.text())
from local_lib.path import Path if __name__ == '__main__': tmp = Path('.') new = tmp / 'test' if not new.isdir(): new.mkdir() new = new / 'test.txt' if not new.isfile(): new.touch() fd = new.open('w') if fd: fd.write('hello') fd.close() if fd: fd = new.open('r') print(fd.read())
#!/usr/bin/python3 # coding : utf8 from local_lib.path import Path if __name__ == "__main__": d = Path('/tmp/yolo') d.mkdir_p() f = Path('/tmp/yolo/swag.gy') f.touch() f.open() f.write_text("YoloSwag") print(f.text())
def fichier(): f = Path('/Users/xmillero/python/d03/D03/ex01/dossier/fichier') f.touch()
#!/usr/bin/python3 # coding : utf8 from local_lib.path import Path if __name__ == '__main__': d = Path('/tmp/djangod03') d.mkdir_p() f = Path('/tmp/djangod03/bidon.txt') f.touch() f.open() f.write_text("Coucou\n", append=True) print(f.text())
def dossier(): d = Path('/Users/xmillero/python/d03/D03/ex01/dossier') d.mkdir_p()
def EcrireLireAfficher(): f = Path('/Users/xmillero/python/d03/D03/ex01/dossier/fichier') f.open() f.write_text("Comme vous pouvez le constater,\nJ'ai réussi l'exercice !\nMais avant de me donner tous les points, il faut vérifier qu'il y a bien la version de pip, le fichier log, la local_lib, le dossier et le fichier dedans avec ce message...") print(f.text())
from local_lib.path import Path if __name__ == '__main__': d = Path('/tmp/golio') d.mkdir_p() f = Path('/tmp/golio/test.txt') f.touch() f.open() f.write_text("Salut tout le monde\nje vends des moutons") print(f.text())
# import sys from local_lib.path import Path def create_dir(p): p.mkdir_p() def create_file(p, my_file, my_content): p2 = p / my_file p2.write_text(my_content) return p2 def read_file(p2, my_file): with p2.open("r") as f: for l in f: print(l) if __name__ == "__main__": my_dir = "/tmp/dir" my_file = "file" my_content = "blabla content blabla" p = Path(my_dir) create_dir(p) p2 = create_file(p, my_file, my_content) read_file(p2, my_file)
from local_lib.path import Path if __name__ == '__main__': d = Path('/home/xubuntu_admin/Desktop/42/d03/ex01/test_dir/') d.mkdir_p() f = Path('/home/xubuntu_admin/Desktop/42/d03/ex01/test_dir/test_file.txt') f.touch() f.open() f.write_text('test text') print(f.text())