我想使用 python 重命名目录中的所有子文件夹。我认为这相当容易,但由于我是 Python 和编程的新手,所以我假设我错过了一些关键的东西。
这是我的文件夹结构:C:\Users\DBailey\Desktop\Here
- 此文件夹有两个文件夹:3303_InfigenSolar 和 3304_FurnaceCanyon
- 在这些文件夹中有 4 个其他子文件夹,在这些文件夹中有一堆文件。
我只想批量重命名3303_InfigenSolar和3304_FurnaceCanyon,以便它们读取3303_InfigenSolar_08315和3304_FurnaceCanyon_08315
到目前为止,这是我的代码:
now = datetime.datetime.now()
month = "0" + str(now.month)
day = now.day
year1 = str(now.year)
year2 = year1[2:]
date = str(month) + str(day) + str(year2)
newname = fn + "_" + str(date)
dir = 'C:\Users\DBailey\Desktop\Here'
folder = os.listdir(dir)
for fn in folder:
print newname
os.rename(fn, newname)
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时 - 只打印一个文件夹(只有两个文件夹,但会添加更多文件夹)并且出现以下错误:
Traceback (most recent call last):
File "<interactive input>", line …Run Code Online (Sandbox Code Playgroud) 我有一个 XML 文件(下面的示例) - 我正在使用 Python 2.7
我想将所有出现的stag更改为读取prod。“stag”一词位于 URL 中。我不想更改整个网址,只是更改单词stag的出现- 请参阅示例:
url=http:// stag /comp/rest/services/editor/hyd/MapServer
想要更改为:
url=http:// prod /comp/rest/services/editor/hyd/MapServer
<Map FullExtent="-136163.9492,444360.64787994,-525467.175315,43020.05517682" InitialExtent="-32989.136772,6307.55418809,-4753.07696,5137.2783653">
<LayerList>
<Items>
<Item ID="17" />
<Item ID="20" IsExpanded="true" Name="Reference Data" Visible="true">
<Items>
<Item ID="30" />
<Item ID="34" VisibleInLayerList="false" />
<Item ID="22" VisibleInLayerList="false" />
<Item ID="41" VisibleInLayerList="false" />
<Item ID="16" />
<Item ID="37" />
<Item ID="24" />
<Item ID="39" />
<Item ID="32" />
<Item ID="28" />
<Item ID="26" />
</Items>
</Item>
<Item ID="19" IsExpanded="true" …Run Code Online (Sandbox Code Playgroud)