Problem with shutil.move - extra \
I have a log file and in the log, so开发者_运维百科me of the files have errors, so when a file has an error I want to read its name (including path) and move it to another directory.
I can read the input file properly, for example one of the files is: C:\test\test1 i can find the file and I just want to move it. However, when I use shutil.move(filename,another_directory), even though printing filename shows c:\test1\test1, shutil somehow appends an extra '\' before every slash.. that is it tries to move C:\\test1\\test1. [Shutil.move is misreading the input path by adding extra '\' to every existing '\' ]
how do i fix it? Thanks !
import shutil
f=open("test_logs")
o=open("output_logs","w")
e=open("error_logs",'w')
another_directory= "C:/test"
for line in f:
if line.lstrip().startswith("C:"):
filename = line
#print line
#look for errors in the next line onwards.
if line.startswith("error"):
e.write(filename + "\n")
print filename
shutil.move(filename,another_directory)
f.close()
o.close()
This is the error i get - IOError: [Errno 2] No such file or directory: 'C:\\test\\test1 (the file is C:\test\test1) and print filename shows c:\test\test1
From the docs on shutil.move
:
If the destination is on the current filesystem, then simply use rename. Otherwise, copy src (with copy2()) to the dst and then remove src.
... in which case, you want to use os.rename
because both files do indeed live on the same file system. Instead of:
shutil.move(filename,another_directory)
Do:
directory, name = os.path.split(filename)
os.rename(filename, os.path.join('c:', 'test', name))
From the error message you're getting, we can surmise shutil.move
is failing because it expects the destination file to already exist, which it doesn't.
Several things are wrong here.
From your edited error messages, I see you're moving C:/test/test1 to C:/test/test1... this will (if I remember correctly) always fail on Windows. Try a different destination directory for testing.
another_directory = "C:\test"
Should be "C:/test" or use os.path.join, etc.
for line in f:
if line.lstrip().startswith("C:"):
filename = line
#print line
if line.startswith("error"):
e.write(filename + "\n")
print filename
shutil.move(filename,another_directory)
Both of these if conditions cannot be true at the same time, so you must be parsing filenames out of previous lines and then checking for "error" in later lines. However, line.lstrip() doesn't modify line, it only returns a new value. When you write the saved value (in filename) to your error log, you have two newlines – one which is still in filename. Then when you move that file, it doesn't exist because "filename" still has both the newline and any leading whitespace you lstrip'd off.
another_directory= "C:/test"
filename = None
for line in f:
if line.lstrip().startswith("C:"):
filename = line.strip() # Remove both leading and trailing WS.
print "File:", filename, "(exists: %s)" % os.path.exists(filename)
elif line.startswith("error"):
assert filename is not None # Do a sanity check as required.
e.write(filename + "\n")
print "Moving %s..." % filename
shutil.move(filename, another_directory)
精彩评论