我已经开始使用GitHub来显示一些非常简单的程序。https://github.com/MasonMcGerry
我想上传更多简单的程序,但它们中的许多都有我在程序中使用的目录和文件的完整和绝对文件路径。
我不想把它放在互联网上,因为那看起来不安全,但也许这并不重要。
我正在考虑创建一个新的项目克隆,然后将路径更改为虚构的内容,如/path/ to /this/file。这似乎会花费更长的时间,而且我会遇到调试问题,因为我会在本地主机上使用绝对文件路径进行编程,测试它们,然后为"GitHub ready/safe“版本重写文件路径。
我的本地主机上的工作版本和“克隆”都在我的计算机上,但只有“克隆”在它的目录中有.git文件。
下面的简单程序示例更改了文件路径。
# for converting tag types to another platforms design
# filepath
fp = '/Users/username/Desktop/Projects/Programs/tagConverter/'
IG = ['IG','ig','instagram','Instagram']
YT = ['YT','yt','youtube','Youtube','Youtube']
end = ['end','END','End','stop','STOP','Stop']
runState = True
# Instagram
def convIG():
# takes convTxt.txt file, strips out commas and puts each item into array
# line by line writes into convIG.txt
# open up file /Users/username/Downloads/tagConverter/convTxt.txt
# and read it
f = open(fp + 'convTxt.txt','r')
inconvIG = f.read()
outconvIG = '#'+("#").join(inconvIG[0:].split(','))
g = open(fp + 'convIG.txt','w')
g.write(outconvIG.replace(' ',''))
g.close()
f.close()
# YouTube
"""
will need ML to separate recognizable words with spaces like NLP
other Instagram tags like '#Howto#DIY' will become CSV howto,diy
instead of the preferred 'How to, DIY'
probably will use 'in' operator to help recognize and separate words
i.e.
s = '#howto'
x = ''
if 'how' in s:
x += 'how '
if 'to' in s:
x += 'to '
print(x)
x == 'how to '
"""
def convYT():
# takes convTxt.txt file, strips out # and puts each item into array
# line by line writes into convYT.txt
# open up file /Users/username/Downloads/tagConverter/convTxt.txt
# and read it
f = open(fp + 'convTxt.txt','r')
inconvYT = f.read()
outconvYT = (",").join(inconvYT[0:].split('#'))
g = open(fp + 'convYT.txt','w')
g.write(outconvYT)
g.close()
f.close()
def prompt():
global runState
prompt = input("Which Tag Type would you like to convert to?\n")
if prompt in IG:
convIG()
elif prompt in YT:
convYT()
elif prompt in end:
runState = False
else:
print('fail')
# indefinite loop, enter an end word listed in 'end' list
while runState == True:
prompt()
"""
SOURCES:
https://stackoverflow.com/questions/2783969/compare-string-with-all-values-in-array
https://stackoverflow.com/questions/18132912/checking-if-a-value-is-equal-to-any-value-in-an-array
https://stackoverflow.com/questions/29101836/typeerror-function-object-is-not-subscriptable-python
Reuben Young colleague and friend
https://www.journaldev.com/23763/python-remove-spaces-from-string
https://www.programiz.com/python-programming/global-keyword
https://help.instagram.com/351460621611097
https://www.w3schools.com/python/gloss_python_global_variables.asp
https://beginnersbook.com/2019/03/python-any-function/
https://www.programiz.com/python-programming/list-comprehension
"""我意识到这段代码非常简单,但随着我的进步,其中一些代码会变得更加复杂,这将是一个麻烦。
有没有解决办法,我是不是多疑了,或者误解了/误用了这项技术(git/github)?
发布于 2020-08-27 23:26:48
您不想公开您的本地文件系统,这是正确的。任何硬编码路径都应该是相对的,以避免让潜在的坏角色洞察你的机器是如何组织的。例外情况是系统默认目录,如C://Progam Files,其中所有windows计算机都存储特定类型的数据。
这里最好的解决方案是将这些文件放入某个resources目录下的项目中,并从那里直接访问它们。您还可以解析配置文件,该文件将指定在何处查找这些文件。例如,
tagConverterPath = "/Users/username/Desktop/Projects/Programs/tagConverter/"当然,您需要将配置文件添加到.gitignore中,并且每个用户都需要定义自己的配置文件,除非您为他们提供了默认的配置文件。
https://stackoverflow.com/questions/63619140
复制相似问题