Does anyone know how to rename a file with Python?
What I do is that the read function ends and then I call the other function to rename it. 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()
I tried to do it with the method os
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'
In principle it should work, although I prefer to use:
On the other hand, to rename a file what I do is:
Comment sa va, the problem is what the error message indicates:
Remember that if you open the file,
you have to close it,
since if you try to open it again, it will cause the error:
, indicating that another process is using the file.
First solve this detail since 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.