跳至正文

自己改的python

  • 随笔

  想在Ubuntu里面弄桌面幻灯片,到网上搜了一下,找到了python文件生成XML的方法,也知道了Ubuntu用XML来使用桌面幻灯片,用了下试试,发现总有一段纯色的时间,没办法,硬着头皮看了看代码,发现python,至少是这个python文件很容易懂,估计问题大概是处在一个."的上面,这样的话会把所有文件枚举一次,就可能把自己枚举或者是那个XML文件(估计是这样,这是第一次接触python。)然后就只留下了".jpg"的内容,奇迹般地就好了,也大概知道了在XML里面设置停顿时间和切换时间。
  不过我批评python一下,我真不喜欢用缩进来写代码的(上面这些都是自己摸索的,没搜资料!)
  原python如下:
#!/usr/bin/env python
# –
– coding: utf-8 –
# Name:  slidexml.py
# Author: EthanZ6174
#        Email: <heracles.621@gmail.com>
#        Site: http://www.noslog.com
# Licence: GPLv3
# Version: 091127

import glob, os
import shutil
import time

curdir = os.getcwd()
os.chdir(curdir)
currentFilelist = glob.glob(
.)
currentImageFiles = glob.glob(
.jpg’)

currentTime = time.localtime()
length = len(currentImageFiles)

for i in currentFilelist:
                if i == ‘backgroundslide.xml’:
                        os.remove(i)

f = file(‘backgroundslide.xml’‘w’)

f.write(‘<background>\n)
f.write(\t<starttime>\n)
f.write(\t\t<year>’ + str(currentTime.tm_year) + ‘</year>\n)
f.write(\t\t<month>’ + str(currentTime.tm_mon) + ‘</month>\n)
f.write(\t\t<day>’ + str(currentTime.tm_mday) + ‘</day>\n)
f.write(\t\t<hour>’ + str(currentTime.tm_hour) + ‘</hour>\n)
f.write(\t\t<minute>’ + str(currentTime.tm_min) + ‘</minute>\n)
f.write(\t\t<second>’ + str(currentTime.tm_sec) + ‘</second>\n)
f.write(\t</starttime>\n)
f.write(‘<!–This animation will start at the time it created–>\n)

for i in currentImageFiles:
        length = length – 1

        f.write(\t<static>\n)
        f.write(\t\t<duration>595.0</duration>\n)
        f.write(\t\t<file>’ + curdir + ‘/’ + currentImageFiles[length] +‘</file>\n)
        f.write(\t</static>\n)
        if length >= 1:
                f.write(\t<transition>\n)
                f.write(\t\t<duration>5.0</duration>\n)
                f.write(\t\t<from>’ + curdir + ‘/’ + currentImageFiles[length] + ‘</from>\n)
                f.write(\t\t<to>’ + curdir + ‘/’ + currentFilelist[length – 1] + ‘</to>\n)
                f.write(\t</transition>\n)

f.write(‘</background>\n)
f.close()



  被我修改的如下:
#!/usr/bin/env python
# –– coding: utf-8 –
# Name: slidexml.py
# Author: EthanZ6174
# Email: <heracles.621@gmail.com>
# Site: http://www.noslog.com
# Licence: GPLv3
# Version: 091127

import glob, os
import shutil
import time

curdir = os.getcwd()
os.chdir(curdir)
currentImageFiles = glob.glob(‘*.jpg’)

currentTime = time.localtime()
length = len(currentImageFiles)
timeforwait = "10.0"
timeforchange = "5.0"

f = file(‘backgroundslide.xml’‘w’)

f.write(‘<background>\n)
f.write(\t<starttime>\n)
f.write(\t\t<year>’ + str(currentTime.tm_year) + ‘</year>\n)
f.write(\t\t<month>’ + str(currentTime.tm_mon) + ‘</month>\n)
f.write(\t\t<day>’ + str(currentTime.tm_mday) + ‘</day>\n)
f.write(\t\t<hour>’ + str(currentTime.tm_hour) + ‘</hour>\n)
f.write(\t\t<minute>’ + str(currentTime.tm_min) + ‘</minute>\n)
f.write(\t\t<second>’ + str(currentTime.tm_sec) + ‘</second>\n)
f.write(\t</starttime>\n)
f.write(‘<!–This animation will start at the time it created–>\n)

for i in currentImageFiles:
        length = length – 1
        f.write(\t<static>\n)
        f.write(\t\t<duration>’+ timeforwait +‘</duration>\n)
        f.write(\t\t<file>’ + curdir + ‘/’ + currentImageFiles[length] +‘</file>\n)
        f.write(\t</static>\n)
        if length >= 1:
                f.write(\t<transition>\n)
                f.write(\t\t<duration>’ + timeforchange + ‘</duration>\n)
                f.write(\t\t<from>’ + curdir + ‘/’ + currentImageFiles[length] + ‘</from>\n)
                f.write(\t\t<to>’ + curdir + ‘/’ + currentImageFiles[length – 1] + ‘</to>\n)
                f.write(\t</transition>\n)

f.write(‘</background>\n)
f.close()

发表评论

您的电子邮箱地址不会被公开。