我在Python中尝试字符串重复.
#!/bin/python
str = 'Hello There'
print str[:5]*2
Run Code Online (Sandbox Code Playgroud)
产量
你好你好
要求的输出
你好你好
任何人都可以指出我正确的方向.
Python版本:2.6.4
有人可以指定unix系统os.path.getmtime(path)
和之间的区别os.path.getctime(path)
.根据python文档中的定义:
os.path.getmtime(path)
返回上次修改路径的时间.返回值是一个数字,给出了自纪元以来的秒数(参见时间模块).如果文件不存在或无法访问,则引发os.error.
os.path.getctime(path)
返回系统的ctime,在某些系统(如Unix)上是最后一次更改的时间,而在其他系统(如Windows)上,是路径的创建时间.返回值是一个数字,给出了自纪元以来的秒数(参见时间模块).如果文件不存在或无法访问,则引发os.error.
这基本上意味着它们在unix/systems中使用时是相同的吗?
#!/usr/bin/python
import os
print os.path.getmtime('File')
print os.path.getctime('FIle')
Run Code Online (Sandbox Code Playgroud)
两个打印都获取相同的值.
我基本上是在寻找文件的最后创建日期,而不是最后修改日期.有没有办法在unix中实现相同的目标?
我在Windows上安装请求模块(python 2.7)时遇到问题.
根据docuemntation尝试以下步骤:
1
pip install requests
错误
'pip' is not recognized as an internal or external command,
operable program or batch file.
2
easy_install requests
错误
'easy_install' is not recognized as an internal or external command,
operable program or batch file.
3
setup.py
错误
C:\Location\Python\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'zip_safe' warnings.warn(msg)
C:\Location\Python\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'include_package_data' warnings.warn(msg)
C:\Location\Python\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'install_requires' warnings.warn(msg)
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...] …
Run Code Online (Sandbox Code Playgroud) 当尝试使用简单的LDAP应用程序连接到LDAP服务器时,我收到一条错误,其中显示"简单绑定失败".我假设这与某种BIND有关.我在其中一个属性文件中有一个绑定属性用于不同的应用程序,但我不知道如何将该属性传递给该程序.
我是否需要添加更多详细信息?
码
import javax.naming.directory.*;
import javax.naming.*;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Properties;
public class SearchLDAP {
public static void main(String[] args) {
String base = "";
String filter = "(objectclass=*)";
Properties env = new Properties();
env.put(DirContext.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(DirContext.PROVIDER_URL,"ldaps://misguided.com.au:343");
try {
System.out.println("11");
DirContext dc = new InitialDirContext(env);
System.out.println("22");
SearchControls sc = new SearchControls();
sc.setSearchScope(SearchControls.OBJECT_SCOPE);
NamingEnumeration ne = null;
ne = dc.search(base, filter, sc);
while (ne.hasMore()) {
SearchResult sr = (SearchResult) ne.next();
System.out.println(sr.toString()+"\n");
}
dc.close();
} catch (NamingException nex) {
System.err.println("Error: " …
Run Code Online (Sandbox Code Playgroud) 我试图将文件从Windows服务器上传到unix服务器(基本上尝试做FTP).我使用下面的代码
#!/usr/bin/python
import ftplib
import os
filename = "MyFile.py"
ftp = ftplib.FTP("xx.xx.xx.xx")
ftp.login("UID", "PSW")
ftp.cwd("/Unix/Folder/where/I/want/to/put/file")
os.chdir(r"\\windows\folder\which\has\file")
ftp.storbinary('RETR %s' % filename, open(filename, 'w').write)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Traceback (most recent call last):
File "Windows\folder\which\has\file\MyFile.py", line 11, in <module>
ftp.storbinary('RETR %s' % filename, open(filename, 'w').write)
File "windows\folder\Python\lib\ftplib.py", line 466, in storbinary
buf = fp.read(blocksize)
AttributeError: 'builtin_function_or_method' object has no attribute 'read'
Run Code Online (Sandbox Code Playgroud)
还MyFile.py
删除了所有内容.
任何人都可以告知出了什么问题.我读过ftp.storbinary用于使用FTP上传文件.
尝试将更改推送到远程存储库时,我收到以下错误.
命令
git push heroku
错误
fatal: You are pushing to remote 'heroku', which is not the upstream of
your current branch 'master', without telling me what to push
to update which remote branch.
任何人都可以建议可能导致相同的原因吗?
我已经读过使用以下格式打开文件时
with open(filename) as f:
#My Code
f.close()
Run Code Online (Sandbox Code Playgroud)
不需要显式关闭文件.有人可以解释为什么会这样吗?此外,如果有人明确关闭文件,它会有任何不良影响吗?
我在运行以下代码时遇到错误.
#!/usr/bin/python
import subprocess
import os
def check_output(*popenargs, **kwargs):
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
location = "%s/folder"%(os.environ["Home"])
subprocess.check_output(['./MyFile'])
Run Code Online (Sandbox Code Playgroud)
错误
subprocess.check_output(['./MyFile'])
AttributeError: 'module' object has no attribute 'check_output'
Run Code Online (Sandbox Code Playgroud)
我正在努力Python 2.6.4
.
我的目标是从文件中读取行,删除它末尾的空格并写回同一个文件.我试过以下代码:
with open(filename, 'r+') as f:
for i in f:
f.write(i.rstrip()+"\n")
Run Code Online (Sandbox Code Playgroud)
这似乎写在文件的末尾,保持文件中的初始数据不变.我知道使用f.seek(0)
将指针返回到文件的开头,我假设这个解决方案需要某种方式.
你能否告诉我是否有不同的方法,或者我是否在正确的补丁上只需要在代码中添加更多逻辑?
我有以下代码:
public class Book {
private static int sample1(int i) {
return i++;
}
private static int sample2(int j) {
return ++j;
}
public static void main(String[] arguments){
int i = 0;
int j = 0;
System.out.println(sample1(i++)); //0
System.out.println(sample1(++i)); //1
System.out.println(sample2(j++));//1
System.out.println(sample2(++j));//2
System.out.println(i);//2
System.out.println(j);//2
}
}
Run Code Online (Sandbox Code Playgroud)
我的预期输出在评论中.实际输出如下:
0
2
1
3
2
2
Run Code Online (Sandbox Code Playgroud)
我对函数调用和incemental运算符感到困惑.有人可以解释实际结果吗?