在python中,我如何加扰文件名和内容以及为什么我的尝试会产生奇怪的结果?

har*_*rry 0 python encryption replace scramble

我正在尝试制作一个脚本,可以在Windows机器上加扰文件夹文件和文件内容.

这是我第一次尝试在文件夹中加密文件名.我知道表现明智它可能很糟糕,它看起来很可怜​​,但我是新的,并试图教给我自己.

import os
import sys
import re
root = 'C:/Users/Any/Desktop/test'

for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' A', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' B', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' C', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' D', '?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' E', '?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' F', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' G', ' ? '))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' H', ' ? '))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' I', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' J', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' K', ' ? '))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' L', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' M', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' N', '?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' O', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' P', '?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' Q', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' R', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' S', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' T', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' U', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' V', ' ? '))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' W', '  ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' X', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' Y', ' ?'))
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace(' Z', ' ?'))
Run Code Online (Sandbox Code Playgroud)

运行脚本之前的文件夹内容是:

FILENAMEABCDEFGHIJKLMNOPQRSTUVWSTXYZ.docx
TEST PICTURE.jpg
TEST SCRIPT.bat
TEST TEXT.txt
Run Code Online (Sandbox Code Playgroud)

运行脚本后:

FILENAMEABCDEFGHIJKLMNOPQRSTUVWSTXYZ.docx
TEST ௷EXT.txt
TEST âŽCRIPT.bat
TESTâ™™ICTURE.jpg
Run Code Online (Sandbox Code Playgroud)

那到底发生了什么?假设如此简单,它怎么能产生这样的结果呢?我应该怎么做才能尝试制作一个加扰脚本,它不一定要提前,因为我想要了解它.

nne*_*neo 6

您的方法存在一些问题.

  1. 每个搜索字符串都以空格开头,因此它只会替换空格和紧跟其后的字符.
  2. 您的替换字符是unicode文字,但您没有在脚本中指定编码(或使用unicode文字).结果可能是来自文本编辑器的UTF-8编码字节被Python解释为latin-1并作为Unicode代码点发送到操作系统 - 即mojibake.
  3. 你正在使用一种非常低效的方法来执行替换.使用.translate字符串的方法并将字符映射表传递给Unicode替换; 然后你只需要遍历你的文件一次,并使用有效的查找而不是一系列冗长的replaces 来执行翻译.每当你发现自己需要复制粘贴一段代码3次或更多次时,问问自己循环或其他技术是否可以更好地工作 - 从来没有任何理由重复26次.
  4. import re但实际上并没有使用它.

这是我编写代码的原因,考虑到上面的所有注释:

import os

# unicode.translate translates *code points* to unicode literals,
# so we apply ord to the letters to get code points
# We also specify our Unicode literals using escape notation to avoid encoding issues.
TRANSTABLE = {
    ord(u'A'): u'\u0123',
    ord(u'B'): u'\u2931',
    # etc
}

# Unicode literal so that os.listdir produces Unicode filenames
# Raw (r) literal so that backslashes are interpreted literally
ROOT = ur'C:\Users\Any\Desktop\test'

for filename in os.listdir(ROOT):
    newname = filename.translate(TRANSTABLE)
    # Don't translate ROOT (avoids translating e.g. the C in C:\)
    os.rename(os.path.join(ROOT, filename), os.path.join(ROOT, newname))
Run Code Online (Sandbox Code Playgroud)