Hide Folders/ File with Python
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"]
.
Related videos on Youtube
Comments
-
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 over 15 years-1: no operating system specified.
-
-
JMax over 11 yearsThis does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
-
ereOn over 11 yearsI 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 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 over 10 yearsctypes does have windll (but only on windows...), see the documentation here docs.python.org/2/library/…
-
Ram Rachum over 10 yearsI get
ImportError: No module named 'ctypes.windll'
on both Python 2.7 and 3.4. This is all on Windows. -
ldrg over 10 yearsThat'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 over 10 yearsYou're right, I was completely wrong.
-
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 almost 7 yearsUsing os.walk I think returns windows-1252 path strings for me, I'm using path.decode("windows-1252") to get the unicode path.
-
nimig18 over 2 years@ldrg does that mean this is wrong stackoverflow.com/a/34040124/3398381 ?