Home > database >  How to hide a folder with a specific path in Python
How to hide a folder with a specific path in Python

Time:02-13

Basically, What I am trying to do is, whenever the assistant is given command that 'hide all the files', it should all the files if a file or folder is open. But, the problem is that if no folder is open, it will hide the current working folder. I want it to search if a folder is open, if not then ask which folder to hide. Please help me to figure this out. Thank You in advance.

The code I am currently using is as following:-

elif "hide all files" in query or "hide this folder" in query:
        try:    
            os.system("attrib  h /s /d")
            speak("Sir, all the files in this folder are now hidden")
        except:
            speak("No directory found.")

CodePudding user response:

Keep a track of the path using some variable.

path = ""

Any query that modifies path should update the variable, and also change the current active directory.

path = updated_path
os.system(f"cd {path}")

For the hide query, you've just got to check if any file/folder is selected.

try:
    if path:    
        os.system("attrib  h /s /d")
        speak("Sir, all the files in this folder are now hidden")
except:
    speak("No directory found.")

CodePudding user response:

Here is the solution for this:-

import os
import plyer
from pathlib import Path
from win32con import FILE_ATTRIBUTE_HIDDEN
from win32api import SetFileAttributes

current_dir = Path().resolve()

def takefolder():  #Takes Folder Input
    while True:
        selected_folder = str(plyer.filechooser.choose_dir())
        selected_folder = selected_folder.replace("[", "")
        selected_folder = selected_folder.replace("]", "")
        selected_folder = selected_folder.replace("'", "")
        selected_folder = " ".join(selected_folder.split())
        if selected_folder=="":
            print("Please select a folder.")
        else:
            break
    return selected_folder

file_path = "Folder_path"
folder_path = os.path.join(current_dir, file_path)

if os.path.isdir(folder_path):
    final_path = folder_path
else:
    print("Please select the folder you want to hide!")
    final_path = takefolder()

try:    
    SetFileAttributes(final_path, FILE_ATTRIBUTE_HIDDEN)
    print("The folder is now hidden!")
except:
    print("No directory found.")
  • Related