How do I move a file to a folder that the code itself created? (python)

Asked

Viewed 193 times

0

Good afternoon, you guys. I wrote a program so that after the user has entered a name, a folder with this name will be created and then a file (from another folder) will be moved to it. The folder is created but I am getting an error while trying to move the file.

import os, shutil

digitar = input()

try:
  if not os.path.exists('G:\\Meu Drive\\help\\jordan\\2. Contas\\'+digitar):
    os.makedirs('G:\\Meu Drive\\help\\jordan\\2. Contas\\'+digitar)
except OSError:
  print ('Error: Creating directory. ' +'G:\\Meu Drive\\help\\jordan\\2. Contas\\'+digitar)

shutil.move('G:\\Meu Drive\\help\\3. Apoio\\0. Templates\\Experiência\\Cópia - TEMPLATE.gsheet', 'G:\\Meu Drive\\help\\jordan\\2. Contas\\'+digitar)

Oserror: [Winerror 87] Incorrect parameter: 'G: My Drive help 3. Support 0. Templates Copy Experience - TEMPLATE.gsheet' -> 'G: My Drive help Jordan 2. Sample accounts'

Why am I getting this error if I’m calling the "type" variable the same way I created the folder? Does anyone have any hints?

I looked at the following questions, because they look like mine, but they didn’t help

https://stackoverflow.com/questions/16868764/how-to-create-folders-and-move-files-based-on-file-name

https://stackoverflow.com/questions/12517451/automatically-creating-directories-with-file-output

https://stackoverflow.com/questions/9717411/python-create-directory-and-move-specific-file

  • look for something about read, write and modify permissions in the folder.

  • I have permission to read, record, modify and even delete the folder.

  • One thing that usually gives problem is the accentuation. Try to do a test using only folders that have no accent, and see what happens. Another thing is that the os.path.exists only checks if it exists, it does not check if it is a directory, so if you have a file called exemplo in that folder, will give error while moving because it is not a directory.

  • A hint, instead of the operator + use os.path.join() to join paths - works multiplatform and it treats the existence of the final bar to you automatically.

  • os.path.exists is there to create the folder with the name coming from the input, which in the case used to demonstrate the error was "example". I tried using os.path.Join() instead of the last operator + in my code, but was accused invalid syntax.

  • @Jordangarcia the use would be like this: os.path.join(r'G:\Meu Drive\help\jordan\2. Contas', digitar)

  • @nosklo I’m still confused. How this way the code will understand that I want to send specifically the file "Copy - TEMPLATE.gsheet" to the newly created folder? I apologize in advance, as this is the first time I use the.path and shutil

  • @Jordangarcia this tip from .join() It’s not related to your problem - it’s just a safer and more direct way of putting together two pieces of a directory path... sorry if it seemed like it would solve the problem. The suggestions I have to solve the problem itself are: 1) try with folders without any accents 2) check if there is a file called exemplo that is hindering the creation of the folder.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.