小编har*_*rry的帖子

Python,如何根据列表重命名多个文件?

在 Windows 中使用 python 我试图一次重命名同一文件夹中的多个文件,但我无法使用列表来进行重命名,这就是为什么当我尝试我的代码时出现此错误:

os.rename(dirlist[1], Words[1]) WindowsError: [Error 2] 系统找不到指定的文件

这是示例代码:

import os
import sys
words = os.listdir('C:/Users/Any/Desktop/test')
dirlist = os.listdir('C:/Users/Any/Desktop/test')

words = [w.replace('E', 'e') for w in words]
print words 

os.rename(dirlist[1], words[1])
Run Code Online (Sandbox Code Playgroud)

我想要实现的目标是让我的 python 脚本在选择的文件夹上运行,该脚本将获取其中的所有文件并重命名所有文件。但棘手的部分是当我无法选出文件夹名称并重命名它们时,因为它们附加到列表中。

python windows rename list

2
推荐指数
1
解决办法
4534
查看次数

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

我正在尝试制作一个脚本,可以在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 = …
Run Code Online (Sandbox Code Playgroud)

python encryption replace scramble

0
推荐指数
1
解决办法
687
查看次数

标签 统计

python ×2

encryption ×1

list ×1

rename ×1

replace ×1

scramble ×1

windows ×1