os.path

python abspath returning path twice

試著忘記壹切 提交于 2021-02-08 08:41:05
问题 I'm trying to get an absolute path of a relative path string with python but it keeps printing the path twice. For example: self.path = 'Users/abdulahmad/Desktop' self.actual_path = os.path.abspath(self.path) print self.actual_path my console prints /Users/abdulahmad/Desktop/Users/abdulahmad/Desktop and if I change the path to: self.path = 'Desktop' my console prints: /Users/abdulahmad/Desktop/Desktop shouldn't it just print /Users/abdulahmad/Desktop in both cases? 回答1: Probably because the

Python idiom to get same result as calling os.path.dirname multiple times?

ぃ、小莉子 提交于 2021-02-07 11:49:57
问题 I find myself needing to get a parent directory of a python file in a source tree that is multiple directories up with some regularity. Having to call dirname many times is clunky. I looked around and was surprised to not find posts on this. The general scenario is: import os.path as op third_degree_parent = op.dirname(op.dirname(op.dirname(op.realpath(__file__)))) Is there a more idiomatic way to do this that doesn't require nested dirname calls? 回答1: Normalize a relative path; os.pardir is

Python idiom to get same result as calling os.path.dirname multiple times?

大兔子大兔子 提交于 2021-02-07 11:49:24
问题 I find myself needing to get a parent directory of a python file in a source tree that is multiple directories up with some regularity. Having to call dirname many times is clunky. I looked around and was surprised to not find posts on this. The general scenario is: import os.path as op third_degree_parent = op.dirname(op.dirname(op.dirname(op.realpath(__file__)))) Is there a more idiomatic way to do this that doesn't require nested dirname calls? 回答1: Normalize a relative path; os.pardir is

Batch File Rename with Python

前提是你 提交于 2021-02-05 12:15:25
问题 Below is my code to batch rename pictures inside a given directory def multi_filename_change(): i = 0 files = askstring('Select your folder', 'Paste your directory path where your images are stored.') for file in os.listdir(files): if not file.startswith('.'): file_name = askstring('Add file name', 'Please enter a name for your files.') src = file dst = file_name + str(i) + ".jpg" os.rename(src, dst) i += 1 When this is run I get the below error message: os.rename(src, dst) FileNotFoundError:

Python 3: search subdirectories for a file

自闭症网瘾萝莉.ら 提交于 2021-01-29 03:16:48
问题 I'm using Pycharm on a Mac. In the script below I'm calling the os.path.isfile function on a file called dwnld.py . It prints out "File exists" since dwnld.py is in the same directory of the script ( /Users/BobSpanks/PycharmProjects/my scripts ). If I was to put dwnld.py in a different location, how to make the code below search all subdirectories starting from /Users/BobbySpanks for dwnld.py ? I tried reading os.path notes but I couldn't really find what I needed. I'm new to Python. import

tarfile can't open tgz

十年热恋 提交于 2021-01-28 09:28:57
问题 I am trying to download tgz file from this website: https://plg.uwaterloo.ca/cgi-bin/cgiwrap/gvcormac/foo07 here is my script: import os from six.moves import urllib import tarfile spam_path=os.path.join('ML', 'spam') root_download='https://plg.uwaterloo.ca/cgi-bin/cgiwrap/gvcormac/foo07' spam_url=root_download+'255 MB Corpus (trec07p.tgz)' if not os.path.isdir(spam_path): os.makedirs(spam_path) path=os.path.join(spam_path, 'trec07p.tgz') if not os.path.isfile('trec07p.tgz'): urllib.request

Incorrect Django path to templates folder in settings.py

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-28 08:04:45
问题 I am newly learning Django and was following the Learn Django 1.11 Tutorial. Here is my current project tree: ├── manage.py ├── muypicky │ ├── __init__.py │ ├── old_settings.py │ ├── settings │ │ ├── base.py # Contains the settings (like shown in the tutorial) │ │ ├── __init__.py │ ├── urls.py │ └── wsgi.py ├── requirements.txt ├── restaurants └── templates # Templates Folder └── index.html I am trying to add the path to the tempelates folder in the settings folder. But the error shown django

Why is os.path.expanduser not returning the home directory?

那年仲夏 提交于 2020-12-05 10:12:25
问题 I am making a python desktop application that saves a log as a .csv file in the user's Documents folder on Windows. The application is written in python 2.7 and kivy 1.8.0, packaged as a Windows program using pyinstaller 2.1, and the installer is made using Inno Setup Compiler. In this post, I will replace the user's real name with USER. I have the following lines of code: DOCUMENTS = os.path.expanduser('~\\Documents\\') print DOCUMENTS with open(DOCUMENTS + 'data_log.csv', 'ab') as f: do

Difference between os.path.dirname(os.path.abspath(__file__)) and os.path.dirname(__file__)

╄→尐↘猪︶ㄣ 提交于 2020-07-31 06:32:06
问题 I am a beginner working on Django Project. Settings.py file of a Django project contains these two lines: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) I want to know the difference as I think both are pointing to the same directory. Also it would be great help if you could provide some links os.path functions. 回答1: BASE_DIR is pointing to the parent directory of PROJECT_ROOT . You can re-write the two

Changing directory from a python script: how to not open a new shell [duplicate]

Deadly 提交于 2020-07-09 06:14:34
问题 This question already has answers here : Change working directory in shell with a python script (5 answers) Why I can't change directories using “cd”? (30 answers) Closed 3 years ago . I have the following piece of code: import os unixshell=os.environ["SHELL"] dir="/home/user/somewhere" if os.path.isdir(dir): os.chdir(dir) os.system(unixshell) This is part of a script I wrote to bookmark folders I visit often in the terminal. A part of the script goes (cd's) to the bookmarked directory. I use