-4

I want do delete a file, for ex. 'myfile.txt' that is stored under a hidden folder. Is it possible to do this in python?

thank you

5
  • 3
    Welcome to SO. Have you researched this question thouroughly, and not found an answer online? Commented Dec 17, 2018 at 14:34
  • 2
    What do you mean by "hidden folder"? Different filesystems implement such a thing in different ways, usually that depends on the operating system, which you do not specify. Commented Dec 17, 2018 at 14:38
  • If you can't see it then it doesn't exists! Commented Dec 17, 2018 at 14:38
  • 2
    @AttilaBognár: that depends on how you are looking Commented Dec 17, 2018 at 14:39
  • My file is in AppData in another folder created by an app. So i'm on windows. For me what i've found so far did not worked. Commented Dec 17, 2018 at 15:04

1 Answer 1

3

Yes, just have the path included the hidden folder as follows:

UNIX:

path = "desktop/.hidden_folder/myfile.txt"

WINDOWS (Code from here):

import os
if os.name == 'nt':
  import win32api, win32con
def file_is_hidden(p):
  if os.name== 'nt':
    attribute = win32api.GetFileAttributes(p)
    return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM)
else:
    return p.startswith('.') #linux-osx
[os.remove(f) for f in os.listdir('.') if file_is_hidden(f)]

Then deal with the file as you wish.

Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.