Posts

Showing posts with the label os module

Working with OS module in Python

import os def soldier(a,b,c): os.chdir(a) i = 1 items = os.listdir(a) for item in items: if item.endswith(b): os.rename(item, item.capitalize()) if item.endswith(c): os.rename(item, f"{i}.{c}") i += 1 fpath = r"D:\abdur codes\space\thisisit\fffffffffffffffff" filename = ".py" format = ".png" soldier(fpath, filename, format) ###################################################################### import os def soldier(fpath, filename, format): os.chdir(fpath) i = 1 files = os.listdir(fpath) with open(filename) as f: # this file contains files names which we will not tamper with filelist = f.read().split("\n") for file in files: if file not in filelist: os.rename(file, file.capitalize()) # if os.splitext(fpath)[1]==format: if os.path.splitext(fil

Deleting a file in Python

We need os module to delete a file on hard drive import os if os.path.exists("myfile.txt"): os.remove("myfile.txt") print("Deleted successfully") else: print("No such file exists")