Rename python file

1

Does anyone know how to rename a file with Python?

What I do is that I finish the read function and then I call the other function to make it known. There is no way to do both at the same time, it tells me that another process is using the file.

This is my code:

import csv

def leer():
m1=open("archivo1.csv","rb")
m1_csv=csv.reader(m1)
var = ""


for i,x in enumerate(m1_csv):


    lista0 = x[0:1]
    lista1 = x[1:2]
    header = str1 = ''.join(lista0)
    header1 = header[9:13]
    str1 = ''.join(lista1)
    str1 = str1[1:]
    str2 = str1[0:32]


    listas = []
    listas.append("The next header ")
    listas.append(header1)
    listas.append("")
    if(header1=="goin"):
        listas.remove("")
        listas.remove("goin")
        listas.append("error")  
        listas.append(" the hash is outdate: "+str2)        
    elif(header1=="omin"):
        listas.remove("")
        listas.remove("omin")
        listas.append("error")  
        listas.append(" the hash is outdate: "+str2)            
    elif( str2 == "074e3e3e82db7610dbeafd95c22d20a2"):
        listas.remove("")
        listas.append(" is using for speaking")
        listas.append(" example bla bla bla the packet")
        listas.append(" Incident: you can get powers")



    listo=["OUTGOING HEADERS"]
    listos=["INCOMING HEADERS"]

    m2_c=csv.writer(open("archivo2.csv","a"),lineterminator='\n')
    if(i==0):
        m2_c.writerow(listo)
    if(i==468):
        m2_c.writerow(listos)
    m2_c.writerow(listas)




m1.close()

Try to do it with the os method but it gives me an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "prueba1.py", line 64, in <module>
    leer()
  File "prueba1.py", line 54, in leer
    os.remove("archivo1.csv")
WindowsError: [Error 32] Le processus ne peut pas accÚde
ier est utilisÚ par un autre processus: 'archivo1.csv'
    
asked by Sergio Ramos 23.07.2016 в 13:55
source

2 answers

1

In principle it should work, although I prefer to use:

with open(file) as csv_data:
     data = csv.reader(csv_data, delimiter=',', quotechar='"')
     # Process data

On the other hand, to rename a file what I do is:

import os
os.rename(old_filename_with_path, new_filename_with_path)
    
answered by 29.07.2016 / 11:51
source
0

Comment sa va, the problem is what the error message indicates:

  

WindowsError: [Error 32] The processus ne peut pas accÚde ier est   use an autre processus: 'file1.csv'

Remember that if you open the file,

m1=open("archivo1.csv","rb")

you have to close it,

m1.close()

because if you try to open it again, it will cause the error:

  

[Error 32] being used by another process

, which indicates that another process is using the file.

First solve this detail as I see not only are you renaming the file, you are actually opening a file and its content to later add this and more content to the file2.csv.

    
answered by 28.09.2016 в 01:35