更改目录名称时的Python递归目录列表

时间:2015-07-02 17:35:29

标签: python python-2.7

所以,我有一个问题,我需要你的python向导帮助

这是我正在使用的目录结构,但它们的名称是加密的。现在,我正在尝试列出这些目录和文件,同时解密它们的名称。

rootfolder
-subfolder
--subsubfolder
---file_in_subsubfolder
--file1_subfolder
-subfolder2
-file1
-file2

这是我目前的代码

for root, dirs, files in os.walk(rootfolder):
    dirname = os.path.basename(root)
    decrypted_dirname = decrypt(dirname)

    print os.path.join(os.path.dirname(root),decrypted_dirname)

    for f in files:
        print os.path.join(os.path.dirname(root), decrypted_dirname, decrypt(f))

我的输出看起来像这样。

rootfolder/decrypted_subfolder
rootfolder/subfolder/decrypted_subsubfolder
rootfolder/subfolder/decrypted_subsubfolder/decrypted_file_in_subsubfolder
rootfolder/decrypted_subfolder2
rootfolder/decrypted_file1
rootfolder/decrypted_file2

问题是,第2行和第3行中的“子文件夹”未解密。如何调整代码,以便我可以解密文件夹并以递归方式正确列出它们。

1 个答案:

答案 0 :(得分:0)

正如@ user3757614所建议的那样,一种方法是使用字典来跟踪"解密的根路径"并重复使用

示例:

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"
];

d = new Date();
    //set the offset
    var utcOffset = -6;
    //get utc
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    //create new date adjusting by the utcOffset to get local date/time
    var localDate = new Date(utc + (3600000 * (utcOffset+1)));
    //check if one character if so format as two by adding leading 0.
    var formatDay = (localDate.getDate().length > 1) ? localDate.getDate() : '0' + localDate.getDate();
    //string everything together
    var fullDate = formatDay + ' ' + monthNames[localDate.getMonth()] + ' ' + localDate.getFullYear() + ' ' + localDate.toLocaleTimeString();

$('#div1').html(fullDate);