[英]Convert list of integers to strings
I need to remove files from all the /etc/rc*.d/ directories.我需要从所有 /etc/rc*.d/ 目录中删除文件。 I can't figure out how to convert integers to strings in an list.
我不知道如何将整数转换为列表中的字符串。
RC = ["3","4","5"]
for RCS in str(RC):
if (os.path.exists("/etc/rc"+RC+".d/file")):
os.remove("/etc/rc"+RC+".d/file")
else:
print "/etc/rc"+str(RC)+".d/file is not a file"
This give me: TypeError: cannot concatenate 'str' and 'list' objects.这给了我: TypeError: cannot concatenate 'str' and 'list' objects。 Appreciate any guidance.
感谢任何指导。
The elements of RC are already strings, so: RC 的元素已经是字符串,所以:
RC = ["3","4","5"]
for RCS in RC:
if (os.path.exists("/etc/rc" + RCS + ".d/file")):
os.remove("/etc/rc" + RCS + ".d/file")
else:
print "/etc/rc"+ RCS +".d/file is not a file"
Your list is in string format.您的列表是字符串格式。 Maybe you meant [3,4,5].
也许你的意思是 [3,4,5]。 You cannot use str or int or float functions on a list.
您不能在列表中使用 str 或 int 或 float 函数。 You need to use the map function to convert a list to string.
您需要使用 map function 将列表转换为字符串。 In your example, the problem is str(RC).
在您的示例中,问题是 str(RC)。 If your list is integer try this:
如果您的列表是 integer 试试这个:
RC = [3,4,5]
for RCS in map(str, RC):
if (os.path.exists("/etc/rc"+RCS +".d/file")):
os.remove("/etc/rc"+RCS +".d/file")
else:
print "/etc/rc"+RCS +".d/file is not a file"
or you can use str() for each element of your list.或者您可以将 str() 用于列表的每个元素。
for RCS in RC:
if (os.path.exists("/etc/rc"+str(RCS) +".d/file")):
os.remove("/etc/rc"+str(RCS) +".d/file")
else:
print "/etc/rc"+str(RCS) +".d/file is not a file"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.