大约有 4,570 项符合查询结果(耗时:0.0255秒) [XML]
python NameError: global name '__file__' is not defined
...
This error comes when you append this line os.path.join(os.path.dirname(__file__)) in python interactive shell.
Python Shell doesn't detect current file path in __file__ and it's related to your filepath in which you added this line
So you should write this line os...
How to use “/” (directory separator) in both Linux and Windows in Python?
...
Use os.path.join().
Example: os.path.join(pathfile,"output","log.txt").
In your code that would be: rootTree.write(os.path.join(pathfile,"output","log.txt"))
...
OS detecting makefile
...different computers and several different operating systems, which are Mac OS X, Linux, or Solaris. For the project I'm working on, I pull my code from a remote git repository.
...
How do I copy an entire directory of files into an existing directory using Python?
...standard shutil.copytree seems arbitrary and annoying. Workaround:
import os, shutil
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree...
Find current directory and file's directory [duplicate]
... directory a Python file is contained in, write this in that file:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is ...
Implement touch using Python?
...han the other solutions. (The with keyword is new in Python 2.5.)
import os
def touch(fname, times=None):
with open(fname, 'a'):
os.utime(fname, times)
Roughly equivalent to this.
import os
def touch(fname, times=None):
fhandle = open(fname, 'a')
try:
os.utime(fname,...
How to get the parent dir location
...can only go as far as the root package, however. If this is a problem, use os.path.abspath: dirname(dirname(abspath(file))).
share
|
improve this answer
|
follow
...
Why doesn't os.path.join() work in this case?
...ath" and everything before them is discarded.
Quoting the Python docs for os.path.join:
If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
Note on Windows, the behaviour in relation to drive letters, which seems ...
Use 'import module' or 'from module import'?
...ur use of it. Here are some points to help you decide.
import module
Pros:
Less maintenance of your import statements. Don't need to add any additional imports to start using another item from the module
Cons:
Typing module.foo in your code can be tedious and redundant (tedium can be minimi...
Batch Renaming of Files in a Directory
...
Such renaming is quite easy, for example with os and glob modules:
import glob, os
def rename(dir, pattern, titlePattern):
for pathAndFilename in glob.iglob(os.path.join(dir, pattern)):
title, ext = os.path.splitext(os.path.basename(pathAndFilename))
...