Hide Folders/ File with Python

25,987

Solution 1

If you don't want to go to the hassle of using pywin32 you can call SetFileAttributes with ctypes in the standard library.

ctypes.windll.kernel32.SetFileAttributesW(path, 2)

path must be a unicode string type as this is the unicode version of SetFileAttributes. The constant 2 is from this page (FILE_ATTRIBUTE_HIDDEN). I imagine that there's no way to get nice constant names out of ctypes so you'll have to look them up yourself.

Solution 2

import tempfile

See the documentation.

Here "hidden file" means "The file is readable and writable only by the creating user ID." i.e., the meaning is "hide file from other users".

Solution 3

is .$filename the kind of thing you're looking for?

Solution 4

If you can put your data in a DBM style file you will only have a single data file.

http://docs.python.org/library/anydbm.html

Instead of filenames you would use keys into the db and the content of your file would be found by indexing into the db.

This requires that your individual files are small enough to be easily fully loaded each time you need access to part of them. If they are big then consider splitting them and using the DBM keys to access chunks of it. For example if "example.txt" contains many lines and you want to be able to access each line individually you could store it as db["example.txt/l1"]db["example.txt/l42"].

Share:
25,987

Related videos on Youtube

d-cubed
Author by

d-cubed

Writing code is good for you.

Updated on April 04, 2020

Comments

  • d-cubed
    d-cubed about 4 years

    Is there any way to hide folders/ files with Python?

    I'm working a huge project (a vulnerability scanner). The project creates a lot of files and folders. Therefore the question, is there any way to make a script that hides files and folders?

    • user1066101
      user1066101 over 15 years
      -1: no operating system specified.
  • JMax
    JMax over 11 years
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
  • ereOn
    ereOn over 11 years
    I honestly don't understand how this question was upvoted 5 times: The question is about hidden files/folders and this short answer is about temporary files. Nothing in the linked documentation is about hidden files and the OP never stated that his files were temporary.
  • jfs
    jfs over 11 years
    @ereOn: I've updated the answer to define what "hidden" means. The OP hadn't elaborated what he mean by "hide" so I took the meaning that makes sense in the context of a vulnerability scanner. btw, in general you don't need to delete files created by tempfile module; it is just how it is often used.
  • ldrg
    ldrg over 10 years
    ctypes does have windll (but only on windows...), see the documentation here docs.python.org/2/library/…
  • Ram Rachum
    Ram Rachum over 10 years
    I get ImportError: No module named 'ctypes.windll' on both Python 2.7 and 3.4. This is all on Windows.
  • ldrg
    ldrg over 10 years
    That's because you don't import ctypes.windll, you import ctypes and call ctypes.windll. Take a look at the source for ctypes in the std library, the importer can't load windll because it's not a submodule of the ctypes package. hg.python.org/cpython/file/844879389a17/Lib/ctypes/…
  • Ram Rachum
    Ram Rachum over 10 years
    You're right, I was completely wrong.
  • ArtOfWarfare
    ArtOfWarfare about 9 years
    @JMax: IDK, it's kind of an attempt at an answer. I'm guessing you tried to flag it as NAA and that flag got rejected.
  • Tahlor
    Tahlor almost 7 years
    Using os.walk I think returns windows-1252 path strings for me, I'm using path.decode("windows-1252") to get the unicode path.
  • nimig18
    nimig18 over 2 years
    @ldrg does that mean this is wrong stackoverflow.com/a/34040124/3398381 ?

Related