Where does "/folder1/folder2/file.ext" save to in Python? - python

I know there must be a really obvious answer, but it eludes me. Where does "/folder1/folder2/file.ext" save to in Python? In some code I thought (incorrectly) that it would save the file to a relative path, but when I checked folder2 it wasn't there. However, it seemed to still take up the hard disk space. I did a search on my hard drive and couldn't find the file. Where did it go? Again, I'm aware that there must be a really obvious answer out there, but I am just learning to code.

Firstly I would read about the os module and the module search path.
I opened the IDLE 3.7.0 on my Windows 10 machine and did the following:
>>> import os
>>> os.getcwd()
'C:\\Python37'
>>> os.path.abspath('./folder1')
'C:\\Python37\\folder1'
Windows expands the . to represent the current working drive, thus wherever the interpreter is looking is where your folders will reside.
In the links, it talks about how sys.path works (in short the directory containing the current script is placed at the front of it [e.g. the current working directory] for searching).
So, in short, either run your script directly from the directory or call os.chdir('/your/desired/path').

Related

Can't import when Path has spaces

Python3.7 on OS X 10.14.5
I have a python script that imports a single local module. They are in the same directory, and everything was working fine. It's a pretty simple setup.
I moved everything over to another directory (on the same machine) and local imports stopped working. The only thing I can tell that has changed is the path, which now contains a folder called "My Drive". I believe the spaces in the path are causing the problem with the import.
I've searched the web, and tried many things, but can't seem to get import to work with a folder in the path that contains spaces. Anyone know any solution to this or a resource to try to figure it out?
I cannot change the folder name.
EDIT:
This was the directory structure:
main.py
lab/
--labs.py
I was able to get it working by flattening out the directory structure. However, when using relative imports:
import lab.labs
or adding 'lab' to the path:
os.path.append("./lab")
import did not work in that location.
Symlinking also works, but that seems like running around python, rather than fixing it within python, and since this is code that will be shared, I don't want a single machine solution.

Setting python path

I have a Django app and I'm getting an error whenever I try to run my code:
Error: No module named django_openid
Let me step back a bit and tell you how I came about this:
I formatted my computer and completely re-installed everything -- including virtualenv, and all dependent packages (in addition to Django) required for my project based on settings in my requirements.txt folder
I tried doing python manage.py syncdb and got the error
I googled the issue, and many people say it could be a path problem.
I'm confused as to how I go about changing the path variables though, and what exactly they mean. I found some documentation, but being somewhat of a hack-ish noob, it kind of goes over my head.
So my questions are:
What exactly is their purpose -- and are they on a system based level based on the version of Python or are they project dependent?
How can I see what mine are set to currently?
How can I change them (ie. where is this .profile file they talk of and can I just use a text editor)
Any input you would have would be great as this one is stumping me and I just want to get back to writing code :-)
The path is just the locations in your filesystem in which python will search for the modules you are trying to import. For example, when you run import somemodule, Python will perform a search for somemodule in all the locations contained in the path (sys.path variable).
You should check the path attribute in sys module:
import sys
print sys.path
It is just a regular list, sou you could append/remove elements from it:
sys.path.append('/path/to/some/module/folder/')
If you want to change your path for every python session you start, you should create a file to be loaded at startup, doing so:
Create a PYTHONSTARTUP environment variable and setting it to your startup file. E.g.: PYTHONSTARTUP=/home/user/.pythonrc (in a unix shell);
Edit the startup file so it contains the commands you want to be auto-executed when python is loaded;
An example of a .pythonrc could be:
import sys
sys.path.append('/path/to/some/folder/')
Do you really need to alter the path? It's always best to actually think about your reasons first. If you're only going to be running a single application on the server or you just don't care about polluting the system packages directory with potentially unnecessary packages, then put everything in the main system site-packages or dist-packages directory. Otherwise, use virtualenv.
The system-level package directory is always on the path. Virtualenv will add its site-packages directory to the path when activated, and Django will add the project directory to the path when activated. There shouldn't be a need to add anything else to the path, and really it's something you should never really have to worry about in practice.

open() function python default directory

I'm new and I have no idea where the default directory for the open() function is.
For example open('whereisthisdirectory.txt','r')
Can someone advise me? I've tried googling it (and looking on stackoverflow) and even putting a random txt file in so many folders but I still can't figure it out. Since I'm beginning, I want to learn immediately rather than type "c:/directory/whatevevr.txt" every time I want to open a file. Thanks!
Ps my python directory has been installed to C:\Python32 and I'm using 3.2
os.getcwd()
Shows the current working directory, that's what open uses for for relative paths.
You can change it with os.chdir.
If you working on Windows OS first type
import os
then type
os.getcwd()
and it should print the current working directory.
The answer is not python-specific. As with programs written in any other language, the default directory is whatever your operating system considers the current working directory. If you start your program from a command prompt window, the CWD will be whatever directory you were in when you ran the program. If you start it from a Windows menu or desktop icon, the CWD is usually defined alongside the program's path when creating the icon, or else falls back to some directory that Windows uses in the absence of that information.
In any case, your program can query the current working directory by calling os.getcwd().
The default location is the CWD (Current Working Directory), so if you have your Python script in c:\directory and run it from there, if you call open() it will attempt to open the file specified in that location.
First, you must import:
import os
Then to print the current working directory:
os.getcwd()
If you want to change the current working directory:
os.chdir('your_complete_required_path')
create the .txt file in the directory where u have kept .py file(CWD) and run the .py file.
The open() function for file always creates files in the current working directory. The best way to find out your current working directory is to find three lines of small code:
import os
current_working_directory = os.getcwd()
print(current_working_directory)
Run this above code and you will get your current working directory where open() function creates a new file. Good Luck!
If you’re running your script through an interpreter (i.e pycharm, VSCode etc) your Python file will be saved, most likely, in my documents (at least in VSCode, in my personal experience) unless you manually save it to a directory of your choosing before you run it. Once it is saved, the interpreter will then use that as you current directory so any saves your Python script will create will also automatically go there unless you state otherwise.
it depends on how you run it from the terminal
like this, it is going to look in your home directory
C:\Users\name>python path\file.py
and like this, it is going to look next to your file
C:\Users\name>cd path
C:\Users\name\path>python file.py

purpose of skeleton directory

I'm just getting into programming; I've made some simple scripts and am hoping to learn how to do things with them. I have a question about where these files "live" on my computer, and if that's important. Let's say I have a script in the directory users/me/desktop/project/skeleton/ called webproject.py. In the skeleton/ directory I also have tests/; does tests/ have to be in skeleton/ or can it be in some random place like my desktop/?
Further, if one of my scripts imports a module that I've created, does it matter where that's located? If I make a script with a function that outputs the nth fibonacci number, and save it on my desktop, can my webproject.py script from users/me/desktop/project/skeleton/ import it?
Any links/resources would be helpful.
It of course matters where you place your files. There sure is more than one directory called "tests", so your Python interpreter cannot guess what "tests" directory it should pick. There is no magic in your computer, I am sorry. ;) But there is something called "Python path". All modules which are saved in a directory specified in the Python path can be imported from anywhere. To use webproject.py from "project/skeleton/" you would have to include "project/skeleton/" in your Python path or make it a package.
There is a nice chapter in the official Python tutorial about modules. :)

finding absolute path from a relative path in python

my question is pretty much what the title suggests. my research has led me to try something like this:
import os
pathname = os.path.abspath("some/relative/directory")
print pathname
this problem is that whenever i do something like this, it simply returns whatever relative directory i gave it preceded by my python directory. for example:
C:\Python27\some\relative\directory
which is not even an existing directory on my computer. i understand that the python interpreter searches the working directory by default, but what i'd like to do is have it search my entire computer for an absolute path that contains the partial directory i specify.
the purpose of this is for me to create an exe (using py2exe) that can search for an arbitrary directory on any computer. is there a method for doing this in the standard library or through some available module - or would i have to implement the algorithm myself?
abspath is based on getcwd. Most likely, your current working directory simply isn't what you expect.
You can change the code that launches your script, change directories manually, or just use chdir in Python.
Did you try os.path.realpath("my/relative/dir")? Actually it seems the directory may not exist, but if it does, it will resolve symbolic links and whatnot.

Categories

Resources