Вопрос

I know there are other questions about renaming, I have looked at a bunch, but my code won't work. I have also seen the OS python definitions.

Basically my code below will be a part of a larger script in GIS that I will be running. I will already have created a geodatabase called permits.gdb and run all the necessary processing on the data, then I want to rename it with the time stamp on it for backup purposes.

I am getting errors that the file can't be found... I just created it in C:\test\permits.gdb so I know it exists.... I have seen several path examples with ", ', /, and \, but none seem to work. Is the path even the problem?

import time
import os

dir_path = os.path.normpath('C:/test')
dir = os.listdir(dir_path)
now = time.strftime('%H%M%A%d%m%Y')

for filename in dir:
    old_name = "permits.gdb"
    new_name = "BAQP_Permits_"+ now +".gdb"
    os.rename(old_name, new_name)

UPDATE: I thank you guys so much for the help! I got it to work but it still throws an error that it can't find the specified file even though it actually does and renames it. I also am now saving it in the location I want and have combined it with the rest of my script (so the file paths are different now) Here is my new code:

dir_path = os.path.normpath('L:\GIS_Admin\SDE_Update_Backup\BAQP_Permits')
dir = os.listdir(dir_path)
now = time.strftime('%H%M%A%d%m%Y')

for filename in dir:
    old_name = dir_path + "\BAQP_Permits.gdb"
    new_name = dir_path + "\BAQP_Permits_"+ now +".gdb"
    os.rename(old_name, new_name)

and my error:

Traceback (most recent call last):
File "C:\NDEPGIS\Scripts\baqp_permits_aris_daily", line 28, in <module>
os.rename(old_name, new_name)
WindowsError: [Error 2] The system cannot find the file specified 
Это было полезно?

Решение 3

Simon got me the code I needed but I had that error I was getting with being unable to find the file even though the file was changed and it was printing my results twice. It was looping without an exit!

dir_path = os.path.normpath('C:\NDEPGIS\Scripts\SDE_Update_Backup\BAQP_Permits')
dir = os.listdir(dir_path)
now = time.strftime('%H%M%A%d%m%Y')

for filename in dir:
    old_name = dir_path + "/BAQP_Permits.gdb"
    new_name = dir_path + "/BAQP_Permits_"+ now +".gdb"
    print "filename", filename
    print "old:", old_name
    print "new:", new_name
    os.rename(old_name, new_name)
    break
print "all done!"

Другие советы

Try this:

import time
import os

dir_path = os.path.normpath('C:/test')
dir = os.listdir(dir_path)
now = time.strftime('%H%M%A%d%m%Y')

for filename in dir:
    old_name = dir_path + "/permits.gdb"
    new_name = dir_path + "/BAQP_Permits_"+ now +".gdb"
    print "filename", filename
    print "old:", old_name
    print "new:", new_name
    os.rename(old_name, new_name)

I don't think filename includes the full path.

Only line 7,8 are different with Simon's code. I think the full path of your data is "C:/test/date_test/permits.gdb", 'old_name' in your code is 'permits.gdb', in Simon's code is 'C:/test/permits.gdb', so you should add dir to 'old_name' and 'new_name'.

    import time
    import os
    dir_path = os.path.normpath('C:/test')
    dir = os.listdir(dir_path)
    now = time.strftime('%H%M%A%d%m%Y')
    for filename in dir:
        old_name = dir_path + '/' + dir + "/permits.gdb"
        new_name = dir_path + '/' + dir + "/BAQP_Permits_"+ now +".gdb"
        print "filename", filename
        print "old:", old_name
        print "new:", new_name
        os.rename(old_name, new_name)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top