0

Is it possible to use mv in Windows python. I want to use mv --backup=t *.pdf ..\ to make copies of existing file but don't want to overwrite them, and Windows move command does not supports suffixes with existing files.

I can run my script with mv command in Windows Bash or CygWin but not on cmd or powershell. So is it possible to use Linux commands in Windows python?

EDIT: i'm using python 2.7

2
  • If you are running windows 10 this is the purpose behind WSL (Windows Subsystem Linux). To turn it on type bash in the terminal. Note that it may not be installed by default and would be in the add windows features section of the control panel Commented Mar 27, 2017 at 14:46
  • 5
    Why not to use the shutil.move()? Commented Mar 27, 2017 at 14:57

2 Answers 2

1

well I tried a different approach to rename the existing files with a random hex at the end on the 'name'

and i'm pretty much satisfied with it :D

if os.path.isfile('../%s.pdf' % name) == True: os.system('magick *.jpg pdf:"%s".pdf' % name_hex) else: os.system('magick *.jpg pdf:"%s".pdf' % name)

Sign up to request clarification or add additional context in comments.

1 Comment

You can mark one of the answers as accepted by clicking on the hollow check mark next to the up/downvotes so it turns solid green. You can undo later if you change your mind.
0

os.rename(src, dst)

Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file.

or shutil.move(src, dst)

Recursively move a file or directory (src) to another location (dst).

If the destination is an existing directory, then src is moved inside that directory. If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics.

If the destination is on the current filesystem, then os.rename() is used. Otherwise, src is copied (using shutil.copy2()) to dst and then removed.

If I got you right both will work for you.

by the way I know that when you install git you can enable Linux commands inside your CMD during the installation. (pay attention to checkbox there), but I'm not sure how it will behave and integrate with your scripts.

3 Comments

os.rename doesn't move across file systems in Windows Python 3.3+. To move across file systems and avoid overwriting an existing file, use shutil.move with dst as the target directory.
In total I have to agree with you, except the issue I don't know which python version @Kamil Mirza using. to cross platfrom between os use os.replace() on Python3. docs.python.org/3/library/os.html#os.replace
os.replace shouldn't be used. Use shutil.move with a try-except. For Windows the exception handler can simply use os.rename in a loop, incrementing the numbered backup name until the rename succeeds; then retry the move. For Unix creating the backup isn't that simple since os.rename replaces the destination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.