使用 Python 隐藏文件夹/文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/549109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide Folders/ File with Python
提问by
Is there any way to hide folders/ files with Python?
有没有办法用 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?
我正在做一个巨大的项目(漏洞扫描器)。该项目创建了许多文件和文件夹。因此问题是,有没有办法制作隐藏文件和文件夹的脚本?
回答by ldrg
If you don't want to go to the hassle of using pywin32 you can call SetFileAttributes with ctypes in the standard library.
如果你不想去使用 pywin32 的麻烦,你可以在标准库中使用 ctypes 调用 SetFileAttributes。
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.
path
必须是 unicode 字符串类型,因为这是 SetFileAttributes 的 unicode 版本。常量 2 来自此页面(FILE_ATTRIBUTE_HIDDEN)。我想没有办法从 ctypes 中获得好的常量名称,所以你必须自己查找它们。
回答by ldrg
If this is for Windows:
如果这是用于 Windows:
http://code.activestate.com/recipes/303343/
http://code.activestate.com/recipes/303343/
Summary: import win32api, win32con, os win32api.SetFileAttributes(filename,win32con.FILE_ATTRIBUTE_HIDDEN)
总结:导入win32api, win32con, os win32api.SetFileAttributes(filename,win32con.FILE_ATTRIBUTE_HIDDEN)
If for Unix:
如果是 Unix:
filename = "." + filename
文件名 = "." + 文件名
for file address in filename use r as prefix because address contains back slashes... eg r"c:...\file"
对于文件名中的文件地址,使用 r 作为前缀,因为地址包含反斜杠...例如 r"c:...\file"
回答by jfs
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".
这里的“隐藏文件”是指“该文件只能由创建用户 ID 读取和写入”。即,意思是“对其他用户隐藏文件”。
回答by neoice
is .$filename the kind of thing you're looking for?
.$filename 是你要找的东西吗?
回答by kmkaplan
If you can put your data in a DBM style file you will only have a single data file.
如果您可以将数据放在 DBM 样式文件中,那么您将只有一个数据文件。
http://docs.python.org/library/anydbm.html
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.
您将使用 db 中的键而不是文件名,并且您的文件内容将通过索引到 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"]
.
这要求您的单个文件足够小,以便在您每次需要访问其中的一部分时轻松完全加载。如果它们很大,那么考虑拆分它们并使用 DBM 键来访问它的块。例如,如果“example.txt”包含多行并且您希望能够单独访问每一行,则可以将其存储为db["example.txt/l1"]
... db["example.txt/l42"]
。
回答by S?awomir Lenart
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.
有可能(至少使用 linux 和 ext fs)打开/创建文件并只保留文件处理程序可用于来自活动进程的读/写操作,但没有其他进程可以看到目录或任何地方列出的文件。
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.
非常不稳定的文件和有点棘手的解决方案,但工作正常。