Reducing a path to simplest value? [duplicate] - python

This question already has answers here:
Canonicalize / normalize a URL?
(6 answers)
Closed 8 years ago.
Is there an os.path function to reduce a path to its simplest form? For example, is there a way to reduce /a/b/c/../../ to /a/?
I've tried os.path.abspath(url) but this prepends my home directory as well. For example, if I want www.google.com/a/index.html it returns /Users/myname/Documents/www.google.com/a/index.html
Obviously one possible solution is to remove /Users/myname/Documents/ from all paths that I create, but I feel like there must be a better way...

You could do something like this:
print(os.path.abspath("/a/b/c/../../") + os.path.sep)
print(os.path.normpath("/a/b/c/../../") + os.path.sep)
# both print /a/
If you work with real paths and python 3.4, you can also use pathlib:
import pathlib
print(pathlib.Path("/tmp/tmux-1000/../").resolve() )

Related

How can I find full path for a file? [duplicate]

This question already has answers here:
How to get an absolute file path in Python
(11 answers)
Closed 2 years ago.
Let's say I have this path
D:\something\something1\from_here_I_now\stuff\stuff2.
So, I know that \from_here_I_now\stuff\stuff2 is a permanent path, but the beginning is different, like I know that D:\something\something1\ may be different for someone else. How can I find the D:\something\something1\ knowing only \from_here_I_now\stuff\stuff2?
Try something like this:
import os
filestr = '\from_here_I_now\stuff\stuff2'
fullstr = os.path.abspath(filestr)
print(fullstr)
>>> 'D:\something\something1\from_here_I_now\stuff\stuff2'
print(fullstr[:len(filestr)])
>>> 'D:\something\something1'

specify more than one pattern with glob - python [duplicate]

This question already has answers here:
Regular expression usage in glob.glob?
(4 answers)
Regular expression, glob, Python
(1 answer)
Closed 4 years ago.
Let's say I have files
bla_foo.txt
bla_Foo.txt
bla_bar.txt
xyz_Bar.txt
foo_bla.txt
is it possible to specify several patterns with glob?
For example how can I select only *Foo.txt, *foo.txt, *bar.txt and *Bar.txt?
I know how to select only *Foo.txt and *foo.txt with glob('*[Ff]oo.txt'). But what if *bar.txt and *Bar.txt are also acceptable? Is this even possible with glob or will I have to create two globs
glob('*[Ff]oo.txt')
glob('*[Bb]ar.txt')

Python path separator [duplicate]

This question already has answers here:
How to get the PATH environment-variable separator in Python?
(5 answers)
Closed 4 years ago.
Is there a proper platform-dependent (or independent?) path separator character somewhere in the Python standard library?
I am not asking about the directory separator / and \, but rather about the PATH separator: : and ;.
Java has java.io.File.pathSeparatorChar and java.io.File.pathSeparator to fulfill this need. Does Python have something similar? A fairly exhaustive search in os.path and pathlib docs yielded nothing, so I am beginning to lose hope.
I am not particularly hung up on the character itself. A function that behaves like os.path.join but for entire paths instead of path elements would be perfectly acceptable, preferable even.
Maybe this is what you're looking for:
import os
os.pathsep
os.pathsep is : or ; while os.path.sep is \ or /.

Retrieving part of URL in python [duplicate]

This question already has answers here:
Python: Get URL path sections
(7 answers)
Closed 5 years ago.
I'm on Python 3.6.
How I can extract part of URL so I can use it as variable.
For example, the URL is http://example.com/comp/project.sec and I would like to get the project part without .sec as variable.
How can I achieve that?
url = "http://example.com/comp/project.sec"
project = url.split("/")[-1].split(".")[0]
Here's an alternative, which removes split requirement.
from os.path import splitext, basename
splitext(basename('http://example.com/comp/project.sec'))[0]
import urllib
urllib.parse.urlparse("http://example.com/comp/project.sec").path.split("/")[-1].split(".")[-1]

use os.system('MyCommand') in the next command [duplicate]

This question already has answers here:
Equivalent of Bash Backticks in Python [duplicate]
(11 answers)
Closed 8 years ago.
As far as you know, we can use OS console commands, (For example dir,time and format in Windows) in Python programing using os.system('TheCommand') module. But this function return the state of Operation (0 for successful and 1 for failed).
I want to know if is there any way to use the output of the commands in the next commands? I mean (For example) I run os.system('dir') and save the list of directories in a variable!
This is fairly easy to do. Here I define the working directory and the edit time of a file as variables which I used later in my script.
#!/usr/bin/env python
PWD = os.getcwd()
edit_time=os.path.getmtime(file.txt)

Categories