Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

OS Module in Python:- 


 It is used to perform the operation in a file under an operating system such as create a directory, change directory, remove directory, rename the file, delete the file, etc.



complete program explanation:-



import os
#os.mkdir("hello")
os.chdir("hello")
#os.mkdir("hello1")
f=open("hello1.txt","w")
f.write("hello world")
f.close()
os.remove("hello1.txt")
#os.rmdir("hello1")
print(os.getcwd())
#print(os.listdir("c:"))
print(os.listdir())
location = 'd:/c/'
for file in os.listdir(location):
   # if file.endswith(".c"):
        print(os.path.join(location, file))



Assignment of os module:-

1)  WAP to count total folder in the computer, drive, max size folder, total image for the particular director, total video on the directory, total python program in the computer.






3 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. Q:- WAP to count total folder in the drive ?
    Solution: -
    import os
    print("The Total Number of Folder's are available in Given Path are : -")
    print(len(os.listdir('C:/')))

    ReplyDelete
  2. import os
    from pathlib import Path

    def endswith1(s):
    if s.index('.')>=0:
    return s[s.index('.'):]
    else:
    return s

    imgcount=0
    txtcount=0
    entries = Path('d:/')
    for entry in entries.iterdir():
    print(entry.name)
    for entry1 in entry.iterdir():
    print(entry1.name)
    try:
    if endswith1(entry1.name)=='png' or endswith1(entry1.name)=='jpg' or endswith1(entry1.name)=='gif':
    imgcount = imgcount+1
    elif endswith1(entry1.name)==".txt" or endswith1(entry1.name)==".doc":
    txtcount = txtcount+1
    except:
    pass


    print("Total images are "+str(imgcount))
    print("Total text files are "+str(txtcount))

    ReplyDelete
  3. import os
    from pathlib import Path

    def endswith1(s):
    if s.index('.')>=0:
    return s[s.index('.'):]
    else:
    return s

    imgcount=0
    txtcount=0
    entries = Path('d:/hello')
    for entry in entries.iterdir():
    print(entry.name)
    if os.path.isdir(entry.name):
    for entry1 in entry.iterdir():
    print(entry1.name)
    try:
    if endswith1(entry1.name)=='png' or endswith1(entry1.name)=='jpg' or endswith1(entry1.name)=='gif':
    imgcount = imgcount+1
    elif endswith1(entry1.name)==".txt" or endswith1(entry1.name)==".doc":
    txtcount = txtcount+1
    except:
    pass
    else:
    if endswith1(entry.name)=='png' or endswith1(entry.name)=='jpg' or endswith1(entry.name)=='gif':
    imgcount = imgcount+1
    elif endswith1(entry.name)==".txt" or endswith1(entry.name)==".doc":
    txtcount = txtcount+1


    print("Total images are "+str(imgcount))
    print("Total text files are "+str(txtcount))

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post