0

The code should create a directory and move the file into it and it works fine when not using Argparse. With Argparse I'm stuck to the point where the directory is not found

CLI error:

Traceback (most recent call last):
  File "/Users/d/PycharmProjects/fileorganizer/addfile.py", line 20, in <module>
    main()
  File "/Users/d/PycharmProjects/fileorganizer/addfile.py", line 17, in main
    crea_cartella("docs", directory)
  File "/Users/d/PycharmProjects/fileorganizer/addfile.py", line 8, in crea_cartella
    os.mkdir(newdirectory)
FileNotFoundError: [Errno 2] No such file or directory: '../fileorganizer/files/docs'

Code:

import os
import argparse

def crea_cartella(nomeCartella, directory):
    newDirectory = os.path.join(directory, nomeCartella)
    os.mkdir(newDirectory)

def main():
    parser = argparse.ArgumentParser(description="sposta un file nella cartella di destinazione")
    parser.add_argument("--nomeFile", type=str, help="nome file compreso di estensione")

    args = parser.parse_args()

    directory = "../fileorganizer/files"
    crea_cartella("docs", directory)

if __name__ == '__main__':
    main()
NeX
  • 1
  • You aren't using `args` (your parsed arguments) at all. Your path is hard-coded to `"../fileorganizer/files"`. Do you have a directory called `fileorganizer` one directory above your current working directory? Presumably you want to pass in another path as the argument to `--nomeFile`? – ChrisGPT was on strike Nov 30 '21 at 17:18
  • --nomeFile is the name of the file I want to move to another directory – NeX Nov 30 '21 at 17:33
  • crea_cartella() should create a folder inside /fileorganizer/files – NeX Nov 30 '21 at 17:34
  • But you're not using it. You need to use `args.nomeFile` somewhere. And I don't see anything here that tries to move a file, just code that tries to create a directory. Again, do you already have a directory called `fileorganizer/` one directory above your working directory? – ChrisGPT was on strike Nov 30 '21 at 17:35
  • yes I have the directory above. For now I don't need to use args.nomeFile because I can't create the new directory – NeX Nov 30 '21 at 17:47
  • So this has nothing to do with argparse. It's just an issue creating a hard-coded directory. Looking closer, you'll need `../fileorganizer/files/` to already exist as well. Does it? – ChrisGPT was on strike Nov 30 '21 at 17:49
  • yes it exists. It's argparse related because the code works without argparse – NeX Nov 30 '21 at 17:53
  • "It's argparse related because the code works without argparse"—that's very unlikely. Are you telling me if you comment out the first four lines of your `main()` function the code starts to work? This error has nothing to do with `argparse`. This code _is_ sensitive to your current directory, though. Are you sure you're in the right directory when you try to run it? – ChrisGPT was on strike Nov 30 '21 at 18:35
  • if I comment the four line it works if I run it with PyCharm but not with the cli – NeX Nov 30 '21 at 18:40
  • Then _it has nothing to do with `argparse`_. If you _don't_ uncomment those lines, I bet it works when you run it with PyCharm. I'm quite sure the issue is that you're in a different directory when you run it in PyCharm vs. the CLI. This code requires that `../fileorganizer/files/` exists relative to whatever your current directory is. – ChrisGPT was on strike Nov 30 '21 at 18:50
  • See [Open file in a relative location in Python](https://stackoverflow.com/q/7165749/354577), for example. – ChrisGPT was on strike Nov 30 '21 at 18:53
  • using `directory = os.path.dirname(__file__) + "/files"` should work – NeX Nov 30 '21 at 19:30
  • Is that supposed to be an answer, or a clarification? You're not using that code above. Please make all information required to answer the question is in the question, not in comments. See [ask]. – ChrisGPT was on strike Nov 30 '21 at 19:43

0 Answers0