11
votes

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?

6
Could you elaborate what do you mean by 'hide folders/files'? - jfs
-1: no operating system specified. - S.Lott
In windows, you could use the %APPDATA% env var as a location to store any files that are not really for the users consumption. - Ben Page
I cast a close vote as unclear what is being asked because this question is receiving answers to two very different questions. The first question could be phrased as "How do I make my file impossible to find for other programs (more than just hiding by prefixing the name with a . on non-Windows.)". The other question could be phrased as "How do I hide a file on Windows?". This question either needs to be closed as unclear, or we should edit the question so that it is clearly asking one of those two questions instead of this ambiguously saying what it does now. - ArtOfWarfare

6 Answers

8
votes

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.

8
votes

If this is for Windows:

http://code.activestate.com/recipes/303343/

Summary: import win32api, win32con, os win32api.SetFileAttributes(filename,win32con.FILE_ATTRIBUTE_HIDDEN)

If for Unix:

filename = "." + filename

for file address in filename use r as prefix because address contains back slashes... eg r"c:...\file"

7
votes
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".

1
votes

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"].

1
votes

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

0
votes

there is possible (at least with linux and ext fs) to open/create file and keep only file handler available for read/write operations from active process, but no other process can see that file listed in directories or anywhere.

it's depends on OS and filesystem, and it is just simple like:

fh = open("data", "w+")
os.system("unlink data")
fh.write(sth)
...

very volatile file and a bit tricky solution, but works fine.