Questions tagged [relative-path]

Relative-paths are URI or directory links to resources that refer to a location relative to the current path.

A relative path references a resource in relation to a current path or directory; its opposite is an absolute path. Web pages frequently make use of relative paths to refer to resources like images and scripts. The advantage of using a relative path is that it remains valid when the host name or root path changes.

As an example, consider a website located at the URL:

http://www.hostname.com/site/page.html

If the page wants to reference an image located at:

http://www.hostname.com/site/images/image.jpg

It could simply make reference to the relative path images/image.jpg.

2470 questions
1668
votes
12 answers

Relative imports for the billionth time

I've been here: http://www.python.org/dev/peps/pep-0328/ http://docs.python.org/2/tutorial/modules.html#packages Python packages: relative imports python relative import example code does not work Relative imports in python 2.5 Relative imports in…
user1881400
1017
votes
11 answers

How to get an absolute file path in Python

Given a path such as "mydir/myfile.txt", how do I find the file's absolute path in Python? E.g. on Windows, I might end up with: "C:/example/cwd/mydir/myfile.txt"
izb
  • 50,101
  • 39
  • 117
  • 168
802
votes
22 answers

Import a module from a relative path

How do I import a Python module given its relative path? For example, if dirFoo contains Foo.py and dirBar, and dirBar contains Bar.py, how do I import Bar.py into Foo.py? Here's a visual representation: dirFoo\ Foo.py dirBar\ …
Jude Allred
  • 10,977
  • 7
  • 28
  • 27
415
votes
21 answers

Relative paths in Python

I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path…
baudtack
  • 29,062
  • 9
  • 53
  • 61
353
votes
25 answers

Convert absolute path into relative path given a current directory using Bash

Example: absolute="/foo/bar" current="/foo/baz/foo" # Magic relative="../../bar" How do I create the magic (hopefully not too complicated code...)?
Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213
243
votes
2 answers

Crontab - Run in directory

I would like to set a job to run daily in the root crontab. But I would like it to execute it from a particular directory so it can find all the files it needs, since the application has a bunch of relative paths. Anyway, can I tell crontab to run…
user333746
  • 2,605
  • 3
  • 18
  • 11
216
votes
10 answers

How to refer to relative paths of resources when working with a code repository

We are working with a code repository which is deployed to both Windows and Linux - sometimes in different directories. How should one of the modules inside the project refer to one of the non-Python resources in the project (CSV files, etc.)? If…
olamundo
  • 23,991
  • 34
  • 108
  • 149
211
votes
14 answers

Open file in a relative location in Python

Suppose my python code is executed a directory called main and the application needs to access main/2091/data.txt. how should I use open(location)? what should the parameter location be? I found that below simple code will work.. does it have any…
user845459
183
votes
14 answers

Resolve absolute path from relative path and/or file name

Is there a way in a Windows batch script to return an absolute path from a value containing a filename and/or relative path? Given: "..\" "..\somefile.txt" I need the absolute path relative to the batch file. Example: "somefile.txt" is located in…
Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
170
votes
12 answers

How to import a CSS file in a React Component

I want to import a CSS file into a react component. I've tried import disabledLink from "../../../public/styles/disabledLink"; but I get the error below; Module not found: Error: Cannot resolve 'file' or 'directory'…
venter
  • 1,890
  • 2
  • 12
  • 15
169
votes
6 answers

relative path in BAT script

Here is my own program folder on my USB drive: Program\ run.bat bin\ config.ini Iris.exe library.dll etc. I would like to use run.bat to start Iris.exe I cannot use this: F:/Program/bin/Iris.exe like a…
user2083037
  • 1,691
  • 2
  • 11
  • 3
157
votes
7 answers

Importing from a relative path in Python

I have a folder for my client code, a folder for my server code, and a folder for code that is shared between them Proj/ Client/ Client.py Server/ Server.py Common/ __init__.py Common.py How do I import…
Drew
  • 12,578
  • 11
  • 58
  • 98
140
votes
9 answers

Using Server.MapPath in external C# Classes in ASP.NET

I'm trying to get the absolute path of certain files in a C# class. Server.MapPath works great of course for ASPX and their code-behind pages, but that doesn't exist in another class file. I tried HostingEnvironment.MapPath(), but that complains…
Chet
  • 21,375
  • 10
  • 40
  • 58
133
votes
6 answers

Reading a file using a relative path in a Python project

Say I have a Python project that is structured as follows: project /data test.csv /package __init__.py module.py main.py __init__.py: from .module import test module.py: import csv with open("..data/test.csv")…
pbreach
  • 16,049
  • 27
  • 82
  • 120
125
votes
7 answers

How to go up a level in the src path of a URL in HTML?

I am storing style sheets in {root}/styles while images in {root}/images for a website. How do I give the path in the style sheets to go look in the images directory for the specified images? e.g. In background-image: url('/images/bg.png');
aateeque
  • 2,161
  • 5
  • 23
  • 35
1
2 3
99 100