Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 脚本读取1行,然后在脚本完成后移到第二行并重复_Python - Fatal编程技术网

Python 脚本读取1行,然后在脚本完成后移到第二行并重复

Python 脚本读取1行,然后在脚本完成后移到第二行并重复,python,Python,下午好 我有一个很奇怪的要求。我正在尝试编写脚本,以帮助为VLAN配置工作。我想让它做的是阅读第1行,完成脚本的其余部分。然后返回阅读第2行。。。完成脚本的其余部分,依此类推,直到完成 这是我当前的配置 file=open("C:\\Users\\KM003308023\\final.txt","w") file.write("") file=open("C:\\Users\\KM003308023\\final.txt","a") interface= open ("C:\\Users\\KM

下午好

我有一个很奇怪的要求。我正在尝试编写脚本,以帮助为VLAN配置工作。我想让它做的是阅读第1行,完成脚本的其余部分。然后返回阅读第2行。。。完成脚本的其余部分,依此类推,直到完成

这是我当前的配置

file=open("C:\\Users\\KM003308023\\final.txt","w")
file.write("")
file=open("C:\\Users\\KM003308023\\final.txt","a")
interface= open ("C:\\Users\\KM003308023\\int.txt")
pull= open ("C:\\Users\\KM003308023\\int.txt")


y= str(input("enter Port description:"))
p= str(input("Enter DATA vlan:"))
x= str(input("Enter voice vlan:"))
portconfig= "\n switchport port-security \n switchport port-security maximum 3 \n switchport port-security aging time 30 \n switchport port-security violation restrict \n switchport port-security aging type inactivity \n no logging event link-status \n no cdp enable \n spanning-tree portfast \n spanning-tree bpduguard enable \n no shut\n!\n"

output=""

for j in pull.readlines():
    output=(output + "default int " + j + "!\n")


for f in interface.readlines():
    output =(output + "int " + f +"description " + y + "\n switchport \n switchport access vlan " + p + "\n switchport mode access \n switchport voice vlan " + x + portconfig)

file.write(output)

file.close()
我想让它做的是

  • 从int.txt中提取readline1
  • 输出默认整数xxx
  • 继续第二部分
  • 第1行
  • 它再次读取行1并输出所需的所有垃圾
  • 电流输出示例:

    default int fa 2/0/15
    !
    default int fa 7/0/1
    !
    default int fa 7/0/8
    !
    int fa 2/0/15
    description UHC_VLAN2
     switchport 
     switchport access vlan 52
     switchport mode access 
     switchport voice vlan 53
     switchport port-security 
     switchport port-security maximum 3 
     switchport port-security aging time 30 
     switchport port-security violation restrict 
     switchport port-security aging type inactivity 
     no logging event link-status 
     no cdp enable 
     spanning-tree portfast 
     spanning-tree bpduguard enable 
     no shut
    !
    int fa 7/0/1
    description UHC_VLAN2
     switchport 
     switchport access vlan 52
     switchport mode access 
     switchport voice vlan 53
     switchport port-security 
     switchport port-security maximum 3 
     switchport port-security aging time 30 
     switchport port-security violation restrict 
     switchport port-security aging type inactivity 
     no logging event link-status 
     no cdp enable 
     spanning-tree portfast 
     spanning-tree bpduguard enable 
     no shut
    !
    int fa 7/0/8
    description UHC_VLAN2
     switchport 
     switchport access vlan 52
     switchport mode access 
     switchport voice vlan 53
     switchport port-security 
     switchport port-security maximum 3 
     switchport port-security aging time 30 
     switchport port-security violation restrict 
     switchport port-security aging type inactivity 
     no logging event link-status 
     no cdp enable 
     spanning-tree portfast 
     spanning-tree bpduguard enable 
     no shut
    !
    
    What i would like to see:
    
    default int fa 2/0/15
    !
    int fa 2/0/15
    description UHC_VLAN2
     switchport 
     switchport access vlan 52
     switchport mode access 
     switchport voice vlan 53
     switchport port-security 
     switchport port-security maximum 3 
     switchport port-security aging time 30 
     switchport port-security violation restrict 
     switchport port-security aging type inactivity 
     no logging event link-status 
     no cdp enable 
     spanning-tree portfast 
     spanning-tree bpduguard enable 
     no shut
    !
    default int fa 7/0/1
    !
    int fa 7/0/1
    description UHC_VLAN2
     switchport 
     switchport access vlan 52
     switchport mode access 
     switchport voice vlan 53
     switchport port-security 
     switchport port-security maximum 3 
     switchport port-security aging time 30 
     switchport port-security violation restrict 
     switchport port-security aging type inactivity 
     no logging event link-status 
     no cdp enable 
     spanning-tree portfast 
     spanning-tree bpduguard enable 
     no shut
    !
    
    等等,直到它用完为止


    我不熟悉这一点,因此仍在尝试获取我的限制。

    您在两个不同的变量中打开同一个文件
    pull
    interface
    。 只需在一个变量中打开它一次

    您不需要将
    input
    的结果键入
    str
    ,因为它返回一个字符串

    您不需要打开文件“final.txt”两次(一次用“w”,第二次用“a”)

    您不应该将输出累积到一个变量中,然后一次将其全部写入

    最后,避免写很长的行;拆分它们以提高可读性

    这提供了(我无法测试它,因为我无法访问您的文件)以下代码:

    y= input("enter Port description:")
    p= input("Enter DATA vlan:")
    x= input("Enter voice vlan:")
    
    portconfig = "\n".join([
      " switchport port-security ",
      " switchport port-security maximum 3 ",
      " switchport port-security aging time 30 ",
      " switchport port-security violation restrict ",
      " switchport port-security aging type inactivity ",
      " no logging event link-status ",
      " no cdp enable ",
      " spanning-tree portfast ",
      " spanning-tree bpduguard enable ",
      " no shut",
      "!\n"
    ])
    
    with open("C:\\Users\\KM003308023\\final.txt","w") as out_file:
      out_file.write("")
      with open ("C:\\Users\\KM003308023\\int.txt","r") as inp_file:
        for j in inp_file.readlines():
          out_file.write("default int " + j)
          out_file.write("!\n")
          out_file.write("int " + j)
          out_file.write("description " + y)
          out_file.write("\n switchport \n switchport access vlan " + p)
          out_file.write("\n switchport mode access \n switchport voice vlan " + x)
          out_file.write(portconfig)
    

    这个问题可能写得不好,但在不添加注释和解释缺失内容以及问题应如何编辑的情况下对其进行向下投票是不礼貌的。您能更好地解释一下这些步骤(1.从int.txt中提取readline1,2.输出默认int xxx,3….,4….,5….)吗。我不明白你想要什么。