Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,隐藏目录的异常行为_Python_Python 2.7 - Fatal编程技术网

Python,隐藏目录的异常行为

Python,隐藏目录的异常行为,python,python-2.7,Python,Python 2.7,我在bing.com上写了一个python脚本来设置我的Ubuntu gnome桌面壁纸 #!/usr/bin/python import urllib import urllib2 import os from bs4 import BeautifulSoup imageurl='http://www.bing.com' page=urllib2.urlopen('http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&

我在bing.com上写了一个python脚本来设置我的Ubuntu gnome桌面壁纸

#!/usr/bin/python
import urllib
import urllib2
import os
from bs4 import BeautifulSoup

imageurl='http://www.bing.com'
page=urllib2.urlopen('http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-IN/')
xml=page.read()
soup=BeautifulSoup(xml,'xml')
imageurl += soup.url.string
imageurl=imageurl[:-12]+'1920x1080.jpg'
print imageurl
try:
    urllib.urlretrieve(imageurl, "wallpaper1.jpg")
    if os.path.isfile('wallpaper.jpg'):
        os.remove('wallpaper.jpg')
    os.rename('wallpaper1.jpg','wallpaper.jpg')
except Exception as e:
    pass
os.system("gsettings set org.gnome.desktop.background picture-uri file:///home/sumit/.myscripts/wallpaper.jpg")
当我从普通目录运行脚本时,它工作得非常好,但当我将脚本复制到隐藏目录(/home/sumit/.myscripts)后使用相同的脚本时,它给出了错误:

Traceback (most recent call last):
  File "/home/sumit/.myscripts/bing.py", line 18, in <module>
    os.rename('/home/sumit/.myscripts/wallpaper1.jpg','/home/sumit/.myscripts/wallpaper.jpg')
OSError: [Errno 2] No such file or directory

我对这种行为感到困惑。如果有人能解释我是python新手,那将非常有帮助。

有一件事我没有得到:stacktrace使用绝对路径显示代码,你说它是有效的。。。顺便说一句,你应该试试shutil.move,而不是delete+rename。有一件事我不明白:stacktrace使用绝对路径显示代码,你说它是有效的。。。顺便说一句,你应该试试shutil.move,而不是delete+rename。正是这样。
#!/usr/bin/python
import urllib
import urllib2
import os
from bs4 import BeautifulSoup

imageurl='http://www.bing.com'
page=urllib2.urlopen('http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-IN/')
xml=page.read()
soup=BeautifulSoup(xml,'xml')
imageurl += soup.url.string
imageurl=imageurl[:-12]+'1920x1080.jpg'
print imageurl
try:
    urllib.urlretrieve(imageurl, "/home/sumit/.myscripts/wallpaper1.jpg")
    if os.path.isfile('/home/sumit/.myscripts/wallpaper.jpg'):
        os.remove('/home/sumit/.myscripts/wallpaper.jpg')
    os.rename('/home/sumit/.myscripts/wallpaper1.jpg','/home/sumit/.myscripts/wallpaper.jpg')
except Exception as e:
    print 'Unable to download'
os.system("gsettings set org.gnome.desktop.background picture-uri file:///home/sumit/.myscripts/wallpaper.jpg")