gpt4 book ai didi

python - 确定目录名称是否超过 260 个字符

转载 作者:行者123 更新时间:2023-12-01 04:32:33 24 4
gpt4 key购买 nike

我导出了一个包含目录和文件的 txt 文件,我试图查明目录是否有 260 个字符或更多。我的脚本的一部分设置了文件的输入、打开文件并循环访问文件。我陷入了在循环中的 if 语句中添加什么内容的困境。我想我应该使用 \ 作为分隔符并在其中搜索以查看是否有任何文本 >= 260。如何构建此脚本?

C:\Program Files\Microsoft Office\Office14\Groove\ToolData\groove.net\GrooveForms4\FormsStyles\GrayCheck

fname = raw_input("Enter filename: ")
fhand = open(fname)
for line in fhand:
# What here?

最佳答案

您可以使用内置的 len() 检查字符串的长度(或者基本上任何有长度的东西)。功能:

fname = raw_input("Enter filename: ")
fhand = open(fname)
for line in fhand:
if len(line) >= 260:
# Do stuff
else:
# Do other stuff

<罢工>

If you would prefer to check if the length of any directory in the path is over 260 characters ,你应该选择 str.split() :

for line in fhand:
directories = line.replace('\\', '/').split('/')
for directory in directories:
if len(directory) >= 260:
# Do stuff
else:
# Do other stuff

我还添加了 str.replace('\\', '/')为了保持一致性:您现在可以使用 / 添加路径或\作为分隔符。

关于python - 确定目录名称是否超过 260 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32172659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com