我想在Linux中建立一个符号链接.我写了这个bash命令,其中第一个路径是我想要链接到的文件夹,第二个路径是编译源.
ln -s '+basebuild+'/IpDome-kernel/kernel /home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal
Run Code Online (Sandbox Code Playgroud)
它是否正确?
下面的代码将不会加入,在调试时,命令不会存储整个路径,只会存储最后一个条目.
os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/')
Run Code Online (Sandbox Code Playgroud)
当我测试它时,它只存储/new_sandbox/
代码的一部分.
我有一个脚本,我想要一个功能与另一个同时运行.
我看过的示例代码:
import threading
def MyThread (threading.thread):
# doing something........
def MyThread2 (threading.thread):
# doing something........
MyThread().start()
MyThread2().start()
Run Code Online (Sandbox Code Playgroud)
我无法正常工作.我宁愿使用线程函数而不是类来实现.
谢谢你的帮助.
这是工作脚本,感谢您的帮助.
from threading import Thread
class myClass():
def help(self):
os.system('./ssh.py')
def nope(self):
a = [1,2,3,4,5,6,67,78]
for i in a:
print i
sleep(1)
if __name__ == "__main__":
Yep = myClass()
thread = Thread(target = Yep.help)
thread2 = Thread(target = Yep.nope)
thread.start()
thread2.start()
thread.join()
print 'Finished'
Run Code Online (Sandbox Code Playgroud) 我正试图让cron调用正确的PATH.当我从shell运行Python脚本时,脚本运行正常,因为它使用了bashrc中设置的PATH,但是当我使用cron时,所有的PATH都没有在bashrc中使用.有没有一个文件我可以输入PATHs为cron像bashrc或从bashrc调用PATHs的方法?
对不起我不认为我说的正确,我可以得到正确的脚本运行(意味着crontab中脚本的PATH不是问题),只是当脚本运行时我运行一个构建,这使用了PATHs设置.bashrc
.当我登录时运行脚本时,.bashrc
PATH会被拉入.由于cron不会在shell中运行,因为它不会拉入.bashrc
.有没有办法在不必编写bash脚本包装的情况下将其拉入?
我正在尝试验证该目标是否公开了https Web服务.我有通过HTTP连接的代码,但我不知道如何通过HTTPS连接.我读过你使用SSL,但我也读过它不支持证书错误.我得到的代码来自python文档:
import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print r1.status, r1.reason
Run Code Online (Sandbox Code Playgroud)
有谁知道如何连接到HTTPS?
我已经尝试了HTTPSConenction,但它响应了一个错误代码,声称httplib没有属性HTTPSConnection.我也没有socket.ssl可用.
我已经安装了Python 2.6.4,我认为它没有编译SSL支持.有没有办法将这个支持集成到较新的python中,而无需再次安装它.
我已经安装了OpenSSL和pyOpenSsl,我从其中一个答案中尝试了以下代码:
import urllib2
from OpenSSL import SSL
try:
response = urllib2.urlopen('https://example.com')
print 'response headers: "%s"' % response.info()
except IOError, e:
if hasattr(e, 'code'): # HTTPError
print 'http error code: ', e.code
elif hasattr(e, 'reason'): # URLError
print "can't connect, reason: ", e.reason
else:
raise
Run Code Online (Sandbox Code Playgroud)
我有一个错误:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/home/build/workspace/downloads/Python-2.6.4/Lib/urllib.py", line 87, in …
Run Code Online (Sandbox Code Playgroud) 我试图获取代码列出文件夹中的所有目录,将目录更改为该文件夹并获取当前文件夹的名称.到目前为止我的代码是在下面,并且不是在工作.我似乎得到父文件夹名称.
import os
for directories in os.listdir(os.getcwd()):
dir = os.path.join('/home/user/workspace', directories)
os.chdir(dir)
current = os.path.dirname(dir)
new = str(current).split("-")[0]
print new
Run Code Online (Sandbox Code Playgroud)
我还在文件夹中有其他文件,但我不想列出它们.我已经尝试了下面的代码,但我还没有工作.
for directories in os.path.isdir(os.listdir(os.getcwd())):
Run Code Online (Sandbox Code Playgroud)
任何人都可以看到我错在哪里?
谢谢
有它的工作,但它似乎有点圆.
import os
os.chdir('/home/user/workspace')
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
dir = os.path.join('/home/user/workspace', dirs)
os.chdir(dir)
current = os.getcwd()
new = str(current).split("/")[4]
print new
Run Code Online (Sandbox Code Playgroud) 我正在写测试,我听到有人说要使用self.assertFalse
而不是assert False
.为什么这样,有什么好处?
我正在尝试使用自动脚本进入最近创建的文件夹.
我在下面有一些代码
import datetime, os, shutil
today = datetime.datetime.now().isoformat()
file_time = datetime.datetime.fromtimestamp(os.path.getmtime('/folders*'))
if file_time < today:
changedirectory('/folders*')
Run Code Online (Sandbox Code Playgroud)
我不知道如何从现在开始检查最新的时间戳.有任何想法吗?
谢谢
我试图将测试的输出记录到文本文件中.我正在使用unittest模块,并希望将结果记录到文本文件而不是屏幕.我在这里有一些脚本来解释到目前为止所尝试的内容.这是测试脚本.
import unittest, sys
class TestOne(unittest.TestCase):
def setUp(self):
self.var = 'Tuesday'
def tearDown(self):
self.var = None
class BasicTestOne(TestOne):
def runTest(self):
TestOne.setUp(self)
self.assertEqual(self.var, 'Tuesday')
class AbsoluteMoveTestSuite(unittest.TestSuite):
# Tests to be tested by test suite
def makeAbsoluteMoveTestSuite():
suite = unittest.TestSuite()
suite.addTest(TestOne("BasicTestOne"))
return suite
def suite():
return unittest.makeSuite(TestOne)
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
我已将此添加到文件中,但它似乎不起作用.
log_file = 'log_file.txt'
sys.stout = sys.sterr = open(log_file, 'w')
return suite >> sys.stout
Run Code Online (Sandbox Code Playgroud)
并且:
log_file = 'log_file.txt'
return suite >> open(log_file, 'w')
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这个命令的几个不同版本.
if __name__ == '__main__':
unittest.main() >> …
Run Code Online (Sandbox Code Playgroud)