13

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?

d-cubed
  • 1,034
  • 5
  • 30
  • 58
  • Could you elaborate what do you mean by 'hide folders/files'? – jfs Feb 14 '09 at 13:33
  • 2
    -1: no operating system specified. – S.Lott Feb 14 '09 at 16:22
  • 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 Feb 26 '13 at 15:16
  • 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 May 12 '15 at 15:03

5 Answers5

9

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.

ldrg
  • 4,150
  • 4
  • 43
  • 52
  • ctypes does have windll (but only on windows...), see the documentation here http://docs.python.org/2/library/ctypes.html#loading-dynamic-link-libraries – ldrg Feb 05 '14 at 23:07
  • I get `ImportError: No module named 'ctypes.windll'` on both Python 2.7 and 3.4. This is all on Windows. – Ram Rachum Feb 06 '14 at 17:31
  • 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. http://hg.python.org/cpython/file/844879389a17/Lib/ctypes/__init__.py#l442 – ldrg Feb 07 '14 at 06:41
  • 1
    You're right, I was completely wrong. – Ram Rachum Feb 07 '14 at 19:01
  • Using os.walk I think returns windows-1252 path strings for me, I'm using path.decode("windows-1252") to get the unicode path. – Tahlor Jun 23 '17 at 14:55
  • @ldrg does that mean this is wrong https://stackoverflow.com/a/34040124/3398381 ? – nimig18 Sep 16 '21 at 20:38
9

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"

Eugene Soldatov
  • 9,755
  • 2
  • 35
  • 43
  • 2
    unfortunately, win32api module is absent in standard shipping version http://stackoverflow.com/questions/227928/whats-win32con-module-in-python-where-can-i-find-it – Tebe Nov 19 '12 at 19:47
7
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".

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 3
    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. – ereOn Nov 17 '12 at 08:43
  • @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. – jfs Nov 17 '12 at 09:20
1

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

kmkaplan
  • 18,655
  • 4
  • 51
  • 65
0

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.

Sławomir Lenart
  • 7,543
  • 4
  • 45
  • 61