Feb-17-2018, 11:50 AM
I want to make a folder in a folder that the program is located in. The chdir() only works for computer-specific directories. any help?
mkdir() help
|
Feb-17-2018, 11:50 AM
I want to make a folder in a folder that the program is located in. The chdir() only works for computer-specific directories. any help?
Feb-17-2018, 01:28 PM
(This post was last modified: Feb-17-2018, 01:28 PM by Gribouillis.)
You can use the
__file__ variable. Here is a way to create a directory named 'foo' with the standard pathlib modulefrom pathlib import Path p = Path(__file__).resolve().parent/'foo' if not p.is_dir(): p.mkdir()
Feb-17-2018, 03:53 PM
(Feb-17-2018, 01:28 PM)Gribouillis Wrote: You can use the i used this def make_file(): os.getcwd() os.chdir('export') os.mkdir(str(name.get()))it works as intended once now its like this Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\HOME-PC\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) File "C:\Users\HOME-PC\Desktop\program_thing\tkinter_program.py", line 20, in make_file os.chdir('export') FileNotFoundError: [WinError 2] The system cannot find the file specified: 'export'
Feb-17-2018, 05:30 PM
(This post was last modified: Feb-17-2018, 05:31 PM by Gribouillis.)
Why not use my code? It is very good. In your code the
os.getcwd() gets the current working directory but it does nothing with it, so you can safely remove this line. The os.chdir('export') tries to change the current directory by going to a directory named 'export', but it fails because there is no such directory. Finally the name.get() in the last statement won't work because the variable name is not defined. Or may be it is a tkinter variable in your program?
Feb-17-2018, 06:02 PM
(Feb-17-2018, 05:30 PM)Gribouillis Wrote: Why not use my code? It is very good. In your code the export does exist and the name.get is part of a folder creation system
Put a print statement around
os.getcwd() def make_file(): print('Current working directory', os.getcwd()) os.chdir('export') os.mkdir(str(name.get()))Execute the program and look in which current working directory your program is running. Look if you can reach from this path 'export'. I think your cwd is at a different path as the directory 'export'. By the way, os.mkdir makes a directory, not a file.If you want to read/write file, use open(filepath) .After you've understood the old school os.path and the difference between relative and absolute paths, you should take a look into the pathlib module.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Feb-17-2018, 06:06 PM
Feb-18-2018, 03:20 PM
(This post was last modified: Feb-18-2018, 03:28 PM by SteampunkMaverick12.)
(Feb-17-2018, 06:02 PM)DeaD_EyE Wrote: Put a print statement around its supposed to be a folder i am trying to make a program that makes a folder. Then you can make and save files in the folder. Its supposed to b a game modding program |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
directory not being created after os.mkdir() | CAD79 | 3 | 4,583 |
Mar-16-2024, 04:00 PM Last Post: deanhystad |
|
OSERROR When mkdir | Oshadha | 4 | 3,493 |
Jun-29-2022, 08:50 AM Last Post: DeaD_EyE |
|
Python Paramiko mkdir command overwriting existing folder. How can i stop that? | therenaydin | 1 | 4,188 |
Aug-02-2020, 11:13 PM Last Post: therenaydin |