Ehsan ebrahimi
Ehsan ebrahimi

Reputation: 51

File Copy in Python to the Local Network

p = "192.168.12.12"
response = os.system("ping " + ip + " -n 1")
if response == 0:
    print("System  is UP !")
else:
    print("System  is DOWN !")
source = input("Enter source file with full path: ")
target = input("Enter target file with full path: ")
target1 = '\\192.168.62.53\d$\cop1.txt'
try:
    copyfile("f:\cop1.txt",target1)
except IOError as e:
    print("Unable to copy file. %s" % e)
    exit(1)
except:
    print("Unexpected error:", sys.exc_info())
    exit(1)
print("\nFile copy done!\n")

My problem in this code is when I write address in hard_code(Like target1) not input when app running the compile show error:

(No such file or directory: '\192.168.62.53\d$\cop1.txt')

But when I enter the address in running, the program completely runs and the file copied.

What's the problem?

Upvotes: 0

Views: 95

Answers (1)

Sherlock70
Sherlock70

Reputation: 579

You need to escape the backslash with another backslash, just as you did (unintentionally) with the one backslash that got printed in the error message.

target1 = '\\\\192.168.62.53\\d$\\cop1.txt'

Another option would be to use triple quotes:

target1 = """\\192.168.62.53\d$\cop1.txt"""

Furthermore it is better to use os.path.join and have python create the correct path.

Upvotes: 1

Related Questions