gpt4 book ai didi

python : removing "\n" from a file name

转载 作者:行者123 更新时间:2023-12-01 04:49:16 27 4
gpt4 key购买 nike

这里我正在创建一个程序来创建一个名为当前目录+url+系统日期和时间.txt的文件关于使用 netloc 仅从 http://www.flipkart.com 中提取 Flipkart创建的文件包含\n。如何从文件中删除\n。

class FileHandler(object):

def create_file(self,url):
print 'creating file....'
flipkart = urlparse(url)
filename = flipkart.netloc



#filename = dns.name.from_text(url).split(3)[1].to_text(omit_final_dot=True)
#filename = dns.name.from_text(url).split('www.')[1].to_text(omit_final_dot=True)
#str.split(sep=None, maxsplit=-1)
print 'file is---'
print filename
#filename = url
dir = os.getcwd()

#print 'Current directory path is-'
#print dirPath


date = datetime.datetime.now()
now = date.strftime("%Y-%m-%d %H-%M")
dirPath = os.path.join(dir,filename)
dirPath.replace("\n","-")
#filenameCreated = dirPath + now
filenameCreated = dirPath + now
#filenameCreated = filenameCreated1.splitlines()
print filenameCreated
f = self.openfile(filenameCreated + '.txt', 'a')
#readFile(f)
return f
#self.writeFile(f)



#defining openfunction
def openfile(self,filename,mode):
try:
open(filename,mode)

except Exception, err:
print("File cannot be opened.")
print(str(err))
return

最佳答案

您可以将 .rstrip('\n') 附加到字符串以删除换行符。

所以你可以尝试这样的事情:

filename = filename.rstrip('\n')

关于 python : removing "\n" from a file name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28762677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com