如何重命名多个hdfs文件到一些其他文件名与序列号,然后将其移动到另一个位置

zfycwa2u  于 2022-12-09  发布在  HDFS
关注(0)|答案(1)|浏览(217)
hdfs dfs -ls user/unknown_test_11
-rw-r--r--   3 svcusprd hdfs            0 2022-01-20 08:14 user/unknown_test_11/_SUCCESS
-rw-r--r--   3 svcusprd hdfs 727288997334 2022-01-20 08:14 user/unknown_test_11/part-00000-961a93d9-45f2-43fd-b594-cd6f2b690e6f-c000.csv
-rw-r--r--   3 svcusprd hdfs    401558518 2022-01-20 07:11 user/unknown_test_11/part-00001-961a93d9-45f2-43fd-b594-cd6f2b690e6f-c000.csv
-rw-r--r--   3 svcusprd hdfs    683031614 2022-01-20 07:10 user/unknown_test_11/part-00002-961a93d9-45f2-43fd-b594-cd6f2b690e6f-c000.csv
-rw-r--r--   3 svcusprd hdfs    693199661 2022-01-20 07:12 user/unknown_test_11/part-00003-961a93d9-45f2-43fd-b594-cd6f2b690e6f-c000.csv

test-001.csv
test-002.csv
test-003.csv
test-004.csv
ia2d9nvy

ia2d9nvy1#

假设newpath存在。否则,请先手动或使用os.mkdir创建它。

import os

oldpath= r'path_to_old_folder'
newpath= r'path_to_new_folder'

files = os.listdir(oldpath)

cnt = 0
for i in files:
    if i.endswith('csv'):
        cnt += 1
        newfilename = 'test-' + (str(cnt).zfill(3)) + '.csv'
        os.rename(os.path.join(oldpath, i), os.path.join(newpath, newfilename))

相关问题