Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何对文件名和内容进行置乱?为什么我的尝试会产生奇怪的结果?_Python_Encryption_Replace_Scramble - Fatal编程技术网

在python中,如何对文件名和内容进行置乱?为什么我的尝试会产生奇怪的结果?

在python中,如何对文件名和内容进行置乱?为什么我的尝试会产生奇怪的结果?,python,encryption,replace,scramble,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(fu

我正在尝试制作一个脚本,可以在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', ' ╔'))
运行脚本之前的文件夹内容为:

FILENAMEABCDEFGHIJKLMNOPQRSTUVWSTXYZ.docx
TEST PICTURE.jpg
TEST SCRIPT.bat
TEST TEXT.txt
运行脚本后:

FILENAMEABCDEFGHIJKLMNOPQRSTUVWSTXYZ.docx
TEST ௷EXT.txt
TEST âŽCRIPT.bat
TESTâ™™ICTURE.jpg

那到底发生了什么?它被认为是如此简单,它怎么能产生这样的结果呢?我应该怎么做才能尝试创建一个置乱脚本,它不必是预先的,因为我想理解它。

您的每个搜索和替换字符串前面都有一个空格,因此它将只匹配空格后的第一个字母。

您的方法存在一些问题

每个搜索字符串都以空格开头,因此它将只替换空格和紧跟其后的字符。 替换字符是unicode文字,但尚未在脚本中指定编码或使用unicode文字。结果很可能是来自文本编辑器的UTF-8编码字节被Python解释为拉丁语-1,并作为Unicode码点(即mojibake)发送到操作系统。 您正在使用一种效率极低的替换方法。使用字符串的.translate方法并将字符映射表传递给Unicode替换;然后,您只需在文件上循环一次,并使用高效的查找而不是一系列冗长的替换来执行转换。当你发现自己需要复制粘贴一段代码3次或更多次时,问问自己循环或其他技术是否能更好地工作——没有任何理由重复26次。 你导入了它们,但实际上没有使用它。 考虑到以上所有注意事项,以下是我编写代码的目的:

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))

嗯,使用字典和适当的循环看看str.translate我试过了,但我得到了一个错误:python原始UnicodeScape编解码器无法解码位置2-3的字节被截断我想我的问题是我不理解这个unicode的东西,这行怎么说?很可能您没有为“\uxxx”指定正确的语法:指定所需字符的Unicode代码点只需要四个十六进制字节。如果要用Unicode对文件名进行置乱,您应该准备好理解Unicode。看到了。刚意识到你可能在写u'C:\Users\Any\…'。这不起作用-将\U解释为转义序列。使用r修饰符,如我在代码中所述:ur'C:\Users\Any\…'