Hello
I am fresh newbie in Python world. I need a python code that just copy my input file with a different name. I just want to test it.

Example.
A simple scipt file : copyfile.py
when I do a command like "python copyfile.py 1.jpg" it would copy the "1.jpg" with a different name "copy1.jpg" .
I just want to copy file with "copy" name before every file I input.

I just want to test it with my PHP code. Thanks.

Recommended Answers

All 7 Replies

import sys, os

if len(sys.argv) != 2:
  print 'Usage: python copy.py <file_to_copy>'
  sys.exit(0)

if sys.platform[:3] == 'win':
  os.system('copy %s %s' % (sys.argv[1], 'copy' + sys.argv[1]))
elif sys.platform[:5] == 'linux':
  os.system('cp %s %s' % (sys.argv[1], 'copy' + sys.argv[1]))

Thanks for reply. I don't know the how to notify this script the input file.. mean I don't know the replacement for <file_to_copy> , that's why I confused.

Please provide what to replace with <file_to_copy>

I got this error :

File "script.py", line 3
print 'Usage: python copy.py <file_to_copy>'
^
IndentationError: expected an indented block

You're getting an Indentation error. I didn't type that out in an editor so the indentation will be off. Go through and re-tab all the lines that should be tabbed.

An example usage: python copy.py my_favorite_image.jpg

Thanks Again.... It works without error.. but the file cannot copy. I am on linux system, It works without error
here is the code :

import sys, os
if len(sys.argv) != 2:
print 'Usage: python copy.py <file_to_copy>'
sys.exit(0)
if sys.platform[:3] == 'win':
os.system('copy %s %s' % (argv[1], 'copy' + argv[1]))
elif sys.platform[:5] == 'linux':
os.system('cp ' % (argv[1], 'copy' + argv[1]))

Sorry now it works perfectly.. I done a mistake.. used tab on 4,8 and 10 line. Used space. and problem solved. Thanks once again.

I don't know how that is not giving you any errors. There is no indentation and you're missing the %s %s after the cp command on the last line.

commented: Nice help +8

Thanks a lot... It is working prefectly.. Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.