0

I am trying to make a script that can scramble a folders files and the files content on a windows machine.

This was my first attempt at trying to scramble file names in a folder. I know performance wise its probably terrible and it looks pathetic but I'm new and trying to teach it to myself.

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', ' ╔'))

The folder content before running the script was:

FILENAMEABCDEFGHIJKLMNOPQRSTUVWSTXYZ.docx
TEST PICTURE.jpg
TEST SCRIPT.bat
TEST TEXT.txt

After running the script:

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

So what the heck happened? It was suppose to be so simple how could it produce results like this? What should I do to try to make a scrambling script, it doesn't have to be advance cause I want to understand it.

4

2 に答える 2

6

あなたのアプローチにはいくつかの問題があります。

  1. 各検索文字列はスペースで始まるため、スペースとその直後の文字のみが置き換えられます。
  2. 置換文字は Unicode リテラルですが、スクリプトでエンコーディングを指定していません (または Unicode リテラルを使用していません)。結果は、Python によって latin-1 として解釈され、Unicode コードポイント (つまり、mojibake) として OS に送信される、テキスト エディターからの UTF-8 でエンコードされたバイトになる可能性があります。
  3. 置換を実行する非常に非効率的な方法を使用しています。文字列の方法を使用して.translate、文字のマッピング テーブルを Unicode 置換に渡します。その後、ファイルを 1 回ループするだけで、長い一連の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))
于 2012-09-17T02:44:07.417 に答える
1

検索文字列と置換文字列の前には空白があるため、スペースの後の最初の文字にのみ一致します。

于 2012-09-17T02:36:39.257 に答える