Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating/Moving file
#1
Hey guys!

I would like a bit of help please.

In short, I'd like
-User input, naming a file
-Automatically add the .txt extension
-If the file exists in the folder <A>, overwrite it
-Then move it to the appropriate folder <B>
-If the file exists in the destination folder <B>, overwrite it

The end goal is to have a .txt file where a dictionary will dump its content into it, acting as a database in the drive/folder I want it to be in.

Here's my code so far. I'm taking into consideration that the fille will be abc.txt
Obviously, I am forcing the user to add the extension manually, which I ultimately don't want to do.
Also, if the file exists, it won't be created or moved which I would like to force and squash.
def create_file():

    input_1_5 = str(input("Creating file / Please enter a filename with its extension: "))
    f = open(input_1_5, "x")

create_file()

def move_file():
    import shutil
    shutil.move("H:/Github/abc.txt", "H:/Github/School-Exercises")

move_file()
Thank you!
Yoriz write Mar-22-2022, 06:20 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Hi :)
To answer your first problem. If you want to create a new file you have two options on how to open it in python so that it will be created. Either use "x" if you just want to create it, but also it will fail if the file already exists. Or you use "w" which will open the file for a write process but also overwrites any existing file.

The second problem you have is that you use the right function ( shutil.move ) but in the wrong way. The way you are using it right now the system will move it as long as there is not an existing file in that folder already. just define the whole path so that it will be overwritten if it already exists:
shutil.move("H:/Github/abc.txt", "H:/Github/School-Exercises/abc.txt")
If you want to automatically add ".txt" to the file name use some sort of string manipulation. There are many different ways on how to do that.
Let's say that your user already has given the file name
input_1_5 = str(input("Creating file / Please enter a filename: "))

# 1. possible way
input_1_5_1 = "%s.txt" % input_1_5  # compatible with python2

# 2. possible way
input_1_5_2 = "{}.txt".format(input_1_5) # also compatible with later versions of python2 (python2.7+)

# 3. possible way
input_1_5_3 = f"{input_1_5}.txt" # it's the same as the above but not compatible with python2

# 4. possible way
input_1_5_4 = input_1_5 + ".txt" # introduced in python3
Reply
#3
(Mar-25-2022, 10:40 AM)ThiefOfTime Wrote: Hi :)
To answer your first problem. If you want to create a new file you have two options on how to open it in python so that it will be created. Either use "x" if you just want to create it, but also it will fail if the file already exists. Or you use "w" which will open the file for a write process but also overwrites any existing file.

The second problem you have is that you use the right function ( shutil.move ) but in the wrong way. The way you are using it right now the system will move it as long as there is not an existing file in that folder already. just define the whole path so that it will be overwritten if it already exists:
shutil.move("H:/Github/abc.txt", "H:/Github/School-Exercises/abc.txt")
If you want to automatically add ".txt" to the file name use some sort of string manipulation. There are many different ways on how to do that.
Let's say that your user already has given the file name
input_1_5 = str(input("Creating file / Please enter a filename: "))

# 1. possible way
input_1_5_1 = "%s.txt" % input_1_5  # compatible with python2

# 2. possible way
input_1_5_2 = "{}.txt".format(input_1_5) # also compatible with later versions of python2 (python2.7+)

# 3. possible way
input_1_5_3 = f"{input_1_5}.txt" # it's the same as the above but not compatible with python2

# 4. possible way
input_1_5_4 = input_1_5 + ".txt" # introduced in python3

Thank you! It works exactly as you said. However, in the context that the user defines its filename, how do I then move this file elsewhere? How do I code its input without knowing it?
Reply
#4
(Mar-25-2022, 04:50 PM)DeadlyKnight Wrote: How do I code its input without knowing it?
You may not know the name, but your program does: it is in the variable input_1_5_1 (or one of the others ThiefOfTime proposed; just choose one). So you can open the file like this:
f = open(input_1_5_1, "w")
f.close()    # Make a habit of it to close everything you opened once you are ready with it.
Oh I see, you put them in functions so the variables are local and not accessible for other functions. So get rid of the functions. Functions are only useful in the following cases:
  • When you are building a large and complex program, you should split your program in parts that are not complex. These parts will be your functions.
  • When you notice you have to copy parts of your code again and again because certain procedures have to be repeated, you had better put this part in a function.
  • When your teacher requires you to use functions. Smile
So if you need to use functions, then make sure these functions return the values you need again. And use parameters to provide these values to the function.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creating dict out of CSV file without the headers ranbarr 6 2,674 May-09-2021, 08:26 PM
Last Post: ranbarr
  Creating Disassembler for a bin file(total beginner) SoulsKeeper 1 2,472 Sep-04-2018, 04:15 PM
Last Post: Larz60+
  Creating a file with variable name but distinct extension Moeniac 1 2,274 Nov-27-2017, 05:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020